Untitled

Published May 23, 2006
Advertisement
Har har har time for another code question!

Given the following code -
struct MyBase {    void foo();};struct MyStruct : MyBase {};class MyClass : MyBase {};void main() {    MyStruct myStruct;    MyClass  myClass;    myStruct.foo(); // 1    myClass.foo();  // 2}

Are either of the commented lines valid?

The more you know :]
Previous Entry Untitled
Next Entry Untitled
0 likes 4 comments

Comments

Programmer16
No because of the private inheritance thing (yea, I know. Stop being technical, right?)

Btw, I replied to your other one (two entries down.)
May 23, 2006 09:55 PM
Mushu
Oh, in the old one the problem was that the call to std::set::erase was removing both the component and its copy. This occurs when you attempt to refile the component with the identifier it already has, creating a "perfect" copy of itself.

The solution to that is to add in a check to see if the new name is different from the old name; if it is, just return the current data and don't do anything else.

But yeah, solving that one was nightmarish :S
May 23, 2006 10:00 PM
Nit
No! foo() is never implemented so you'll end up with an unresolved external symbol. And also worth noting that structs are the same as classes with the exception that they default to public members and methods, whereas classes default to private. Since the parent class is not extended with the public keyword, we will end up with the conclusion given above.
May 24, 2006 08:37 AM
Mushu
ANSWER!

Not only do structs default to public members, they also default to public inheritance. Therefore line 1 is valid. Classes, as Nit mentioned, default to private, so line 2 will raise an error. Yay for obscurities!

Moral of the story - don't arbitrarily decide to randomly change a 'struct' keyword to a 'class' keyword, unless you want to go "O_o" over your code for a while.
May 24, 2006 12:55 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Untitled

5360 views

Untitled

1048 views

Untitled

1191 views

Untitled

1106 views

Untitled

1151 views

Untitled

1437 views

Untitled

1103 views

Untitled

1004 views

Untitled

1010 views

Untitled

1188 views
Advertisement