reference to parent inherritance?

G

Gernot Frisch

Is there any way to get a reference of parent class in default c'tor?

Like this:

class foo
{
class bar
{
foo& m_f;
public:
bar(const foo* f) : m_f(*f) {}
}
m_bar(this);
};

int main()
{
foo poo;
return 0;
}


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
G

Gernot Frisch

Ian Collins said:
No. you have to be explicit in C++, unlike Java.

Don't know about java. I have a class "foo" that has a "helper-class"
"bar". It's just to bring somehierarchy in a large set of functions.
I'll guess I have to do it with a dynamical allocation of m_pBar the
c'tor of foo then.
Thank you,
-Gernot
 
I

Ian Collins

Gernot said:
Don't know about java. I have a class "foo" that has a "helper-class"
"bar". It's just to bring somehierarchy in a large set of functions.
I'll guess I have to do it with a dynamical allocation of m_pBar the
c'tor of foo then.

I think adding a reference to the outer class is one of things proposed
for the next revision of the standard.
 
G

Gernot Frisch

Ian Collins said:
I think adding a reference to the outer class is one of things
proposed
for the next revision of the standard.

Would be nice - but my life does not depend on it.
 
P

peter koch

Ian said:
I think adding a reference to the outer class is one of things proposed
for the next revision of the standard.
Certainly it is not. For one thing, an inner class could very well be
stand alone.

/Peter
 
?

=?ISO-8859-1?Q?Martin_Vejn=E1r?=

Gernot said:
Is there any way to get a reference of parent class in default c'tor?

Like this:

class foo
{
class bar
{
foo& m_f;
public:
bar(const foo* f) : m_f(*f) {}
}
m_bar(this);
};

int main()
{
foo poo;
return 0;
}

You nearly got it right.

class foo
{
class bar
{
foo & m_f;
public:
bar(foo & f) : m_f(f) {}
} m_bar;
public:
foo() : m_bar(*this) {}
};

My compiler, however, gives me a warning. See
[http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.7].
 
G

Gernot Frisch

You nearly got it right.

class foo
{
class bar
{
foo & m_f;
public:
bar(foo & f) : m_f(f) {}
} m_bar;
public:
foo() : m_bar(*this) {}
};

This is exaclty what I needed. Yeee-haaaaa!
 
I

Ian Collins

peter said:
Certainly it is not. For one thing, an inner class could very well be
stand alone.
Sorry, too many dead brain cells.

I was thinking of granting a nested class access to its containing class
private members, which some compilers do as an extension.
 
I

Ian Collins

Gernot said:
This is exaclty what I needed. Yeee-haaaaa!
Sorry I gave you some wrong advice, I was answering "Is there any way to
get a reference of parent class in default c'tor?" and I didn't spot
that you had a non-default constructor in your example :)
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top