• entries
570
2427
• views
217151

# Untitled

70 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 :]

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

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

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

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.

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.

## Create an account

Register a new account