C++ derived question

Started by
4 comments, last by Cosmic314 20 years, 8 months ago
I am trying to find out what the correct behavior of the following code for the latest ANSI C++. I have a base class and a derived class. In my base class I specify a variable to be used by all derived classes. The base class is abstract and specifies a virtual access function ''AccessSomeVar'' (which in my scenario I never override). In my derived class constructor I set the base class variable in the code section of the constructor (ie not with the initializer). Now in my test code, I instantiate the derived class and then have a base class pointer assigned to it. When I call ''AccessSomeVar'' via the pointer, on one compiler I get an uninitialized value for the variable and on a second compiler I get the correct value (it is non-zero too, so no mysterious compiler initializations occur). What is the "correct" behavior defined for ANSI C++? Thanks.
Advertisement
Let''s see if I have your thingy right. Code would really help here. Tell me if I got it wrong at all.

class Base{public:    int i;    virtual int& AccessSomeVar() { return i; }};class Derived : public Base{public:    Derived() { i = 3; }};int main(){    Base* b = new Derived();    cout << b->AccessSomeVar();}

In that code, 3 should always be printed (though it is bad form to initialize a Base data member in the Derived constructor). Do I have your code right?

How appropriate. You fight like a cow.
Yeah, that''s the code. Although I put the access var as a reference parameter rather than a return type.

I think I may have answered my own question actually. I did not realize you could put code into the parameter passed through the initializer.

I need to learn how to make a code window like you did for the code. Is there a FAQ on that or is it simply HTML?

class Base
{
public:
Base( int x, int y, int z );
virtual void GetZValue( int &z );
....
protected:
int internalz;

private:
};

class Derived : public Base
{
public:
Derived( int x, int y );
...
};

...
int PickZ( int x, int y )
{
if( x > y )
return x;
else
return y;
}

Derived::Derived( int x, int y )
: Base( x, y, PickZ( x, y ) )
{
}

Your hint about "initializations don''t belong in constructors" put me on the correct track. Instead of assigning z inside the Derived constructor, I initialize the base constructor with a value dependent on the x, y parameters. The code works on my one compiler but I don''t have access to the 2nd one yet.

Should the PickZ function be moved to its own namespace as a matter of good programming to make it only visible by the Derived class?

Thanks.
quote:
I initialize the base constructor with a value dependent on the x, y parameters.
Kudos. That''s MUCH better.

quote:Should the PickZ function be moved to its own namespace as a matter of good programming to make it only visible by the Derived class?
A better idea would be to make it a static member function of Derived.

How appropriate. You fight like a cow.
Sneftel,

Thanks for your help. Your advice on the forums is great. I suspected there would be a way to privatize it on my class but couldn''t put my finger on it. I''ll make it static and then my code will be good and happy.

Thanks!
Use "source" html keyword with brackets to create that code window.

Also, if you inherit from an indirect virtual base class(es) your most derived object''s constructor must call all indirect virtual base object''s constructors or otherwise the inheriting data won''t be init and will hold garbage. The constructors in your direct base class will be ignored if they inherit virtually. It''s one of the special cases for virtual inheritance, though you can ignore virt.inh. but then you need to specify member to use by using scoping resolution op because of ambiguity.

This topic is closed to new replies.

Advertisement