Iheritance

N

nick.stefanov

I have an iherited child class in which I would like to have a variable
which uses the base(parent) class. Something like this:

class Child : public Base
{
public:

Child () : Base()
{
}
private:
MyClass *var1;


MyClass has a Base class variable and also uses the Base class in its
constructor like so:

class MyClass {
public:
MyClass(char* Name, Base* base);
private:
char* name:
Base* base;
}

Is is possilbe to instantiate MyClass's base variable with Child's Base
varaible, something like this in Child.cc

Child::Child( ):Base(basename) {

var1 = new MyClass("whatevername", child's alrady instantiated base?)

Thanks

Nick
 
D

David Wilkinson

I have an iherited child class in which I would like to have a variable
which uses the base(parent) class. Something like this:

class Child : public Base
{
public:

Child () : Base()
{
}
private:
MyClass *var1;


MyClass has a Base class variable and also uses the Base class in its
constructor like so:

class MyClass {
public:
MyClass(char* Name, Base* base);
private:
char* name:
Base* base;
}

Is is possilbe to instantiate MyClass's base variable with Child's Base
varaible, something like this in Child.cc

Child::Child( ):Base(basename) {

var1 = new MyClass("whatevername", child's alrady instantiated base?)

Thanks

Nick

Nick:

What does "Child's Base varaible" mean? Child has only a var1 member
variable. What are you really trying to do here?

David Wilkinson
 
N

nick.stefanov

Sorry I mean Child's Base class not variable. When I instantiate var1
i am trying to point it's Base variable to Child's alreay insantiated
Base class. Something like this:

ar1 = new MyClass("whatevername", this)
 
D

David Wilkinson

Sorry I mean Child's Base class not variable. When I instantiate var1
i am trying to point it's Base variable to Child's alreay insantiated
Base class. Something like this:

ar1 = new MyClass("whatevername", this)

nick:

I'm still not sure what you are trying to do. It would really be helpful
if you showed the definitions for all the classes, with constructors
containing all the arguments you need to do what you want.

But perhaps what you are after is indeed:

var1 = new MyClass("whatevername", this);

Did you try it?

David Wilkinson
 
N

Nishant Sivakumar

Did you do this :

Child () : Base()
{
var1 = new MyClass("------", this);
}
 
R

RalphTheExpert

I have an iherited child class in which I would like to have a variable
which uses the base(parent) class. Something like this:

class Child : public Base
{
public:

Child () : Base()
{
}
private:
MyClass *var1;


MyClass has a Base class variable and also uses the Base class in its
constructor like so:

class MyClass {
public:
MyClass(char* Name, Base* base);
private:
char* name:
Base* base;
}

Is is possilbe to instantiate MyClass's base variable with Child's Base
varaible, something like this in Child.cc

Child::Child( ):Base(basename) {

var1 = new MyClass("whatevername", child's alrady instantiated base?)

Thanks

Nick

Doesn't
var1 = new MyClass("whatevername",this);
work?

Or if you want to make the code "clearer" (assuming that Base has at
least one virtual function)
var1 = new MyClass("whatevername", dynamic_cast<Base*>(this));

The dynamic_cast is checked at runtime so there is runtime overhead.

I wish C++ has a static type check that says "Use Base pointer if the
class has a base class of Base".

In my code, as a matter of style and as a hint to the next programmer,
I write
var1 = new MyClass
(
"whatevername",
// char * Name
this
// Base* base
);
At least this gives a hint as to what is happening.


Does my explanation help?

Ralph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top