Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    570
  • comments
    2427
  • views
    217369

Untitled

Sign in to follow this  
Mushu

80 views

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 :]
Sign in to follow this  


4 Comments


Recommended Comments

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

Btw, I replied to your other one (two entries down.)

Share this comment


Link to comment
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

Share this comment


Link to comment
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.

Share this comment


Link to comment
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.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!