Syntax I've never seen before

Started by
16 comments, last by codingo 10 years, 7 months ago

in that sence couldn't you say the same thing about C in its entirety since C++ is a subset of C?

What kind of nonsense is that?

Very brief history lesson:

C had many dialects. The original was written mostly by Dennis Ritchie at Bell labs from 1969-1973. The language was applied in several places and was customized and changed around. In 1979 there was a book attempting to standardize the language, as K&R C. The book was written by the two who contributed most to the language at that time.

The C++ language was one of the many dialects, called "C with Classes", used internally by AT&T. It was a dialect of the language.

Note that "C with Classes" was not a proper subset of K&R C. Even though they shared a common base and most of the trivial K&R C code could be made to compile with it, they were not absolutes. You could write K&R C programs that would not complile in C with Classes, and you could write C with Classes code that would not compile in K&R C.

Even back then, in 1980, neither language was a subset of the other.

In 1983 when the C language began to be standardized by ANSI, the "C with Classes" version was renamed C++. When C++ compilers hit the public in 1985 the language was a sibling of ANSI C, neither had a subset/superset or parent/child relationship. They were similar but distinct.

The two languages have a similar root from the 1970's, but neither language has ever been a subset of the other. They are similar, and many constructs are the same, but in no way is C a subset of C++, nor the other way around. The two languages have always been separate and distinct.

Advertisement
The two languages have always been separate and distinct.

ok, I was lead to believe that C++ was a direct derivation of C.

That threw me off as well as the idea that C and C++ get along so well and that I had never seen that syntax style before.

But really, why re-establish the data type within the constructor method when the data type was determined in the declaration?

Because the right hand side of an assignment needs to be a compatible type with the left hand side of an assignment. Just as you can't assign an int to a string, you can't assign a regular string literal to a wstring.

ok to summ it all up, please correct me if I am wrong.


wstring d_LicenseFilePath;

declares a wide string var...


L""

marks the string literal L as a wide character string literal and...


d_LicenseFilePath = L""

assigns the value L to "d_LicenseFilePath" var which contains only wide string literals

in that sence couldn't you say the same thing about C in its entirety since C++ is a subset of C?

Even though alot of C syntax compiles properly in C++, some stuff isn't used in the common everyday C++ that is more common among C users. The stuff that, while functional in C++, isn't normally used (purely subjectively on my part), I tend to think of as more for 'C' backwards compatibility.

For example:


typedef struct MyStruct
{
    //...
};

What is the 'typedef' there for? In C++, normally, unless you're being backwards compatible, you just type 'struct MyStruct'.

Again, subjective on my part. My only point is that L"" isn't there just for backwards compatibility with C, it's there because you need to be able to define wide-character literals in C++ during normal C++ usage (if you need wide characters).

"string literal" becomes const char*

L"string literal" becomes a const wchar_t*

Since they are different types, you can't (and shouldn't be able to, to avoid errors) assign a const char* to a std::wstring, so if you wanted to, within your code, assign a literal to a wstring (or to a wchar_t pointer), you'd need some way to create literals of that type, and that's what the L prefix is for.

In C++11, two more string types were defined:

std::u16string, which manages strings composed of char16_t*.

You can declare raw char16_t string literals like this: u"string literal" (a const char16_t*)

std::u32string, which manages strings composed of char32_t*.

You can declare raw char32_t string literals like this: U"string literal" (a const char32_t*)

Which leads to the question: if "wstring d_LicenseFilePath;" was declared as a private member var of the class, why establish the wstring literal type again in the constructor?

You're correct that 'd_LicenseFilePath' was already initialized to an empty string by default, so that redundant initialization isn't necessary.

ok to summ it all up, please correct me if I am wrong.

wstring d_LicenseFilePath;
declares a wide string var...

Yes, it defines a std::string-like class that takes wchar_t* instead of char*.
Both std::string and std::wstring are just typedefs of the template class std::basic_string.

L""
marks the string literal L as a wide character string literal and...

Yep.

d_LicenseFilePath = L""
assigns the value L to "d_LicenseFilePath" var which contains only wide string literals

The 'L' isn't anything except to tell the compiler that the following string literal is to be a const wchar_t* instead of const char*.
L"" is the same as "", except in wide characters.
L"Meow" is the same as "Meow", except in wide characters.

L"" is an empty wchar_t* string. The L isn't any part of the string itself.

oh i c, thank you Servant of the Lord. In other words: A redundancy of no perceivable consequence:)

Thank you Servant of the Lord. I'm starting a Knights of St. John chapter. If you would like to join, just wish it so:)

This topic is closed to new replies.

Advertisement