C++ polymorphism question

J

Jonas Huckestein

Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);
};

class B : A {
void emil(){};
void oskar(void)
{emil();};};

A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.

Regards,
Jonas
 
G

Gianni Mariani

Jonas said:
Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);
};

class B : A {
void emil(){}; the ';' not needed
void oskar(void)
{emil();};};
the ';' after the function is not needed ... don't put it at the end of
functions.
A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.

This is allowed. Not only is it allowed, it is the intention for
virtual functions to have access to the class it is defined in.

Don't call it from "A"'s constructor though, you'll get surprises.
 
G

Guest

Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);
};

class B : A {
void emil(){};
void oskar(void)
{emil();};};

A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.

As D. Susman pointed out, this does not look like what you asked for,
perhaps you meant something like this:

class A {
virtual void foo();
};

class B : public A {
void foo() { bar(); }; // Call B::bar() in B::foo()
void bar();
};

int main() {
A* a = new B;
a->foo(); // This will call B::bar();
}
 
B

Barry

Gianni said:
the ';' after the function is not needed ... don't put it at the end of
functions.

not "not needed", but even wrong, but I'm sure the standard allow extra
';' in class definition or not; AFAIK, some library like /CppUnit/ even
borrows some technique to avoid "extra ';' error" for some compilers,
what compile(s)? I don't know.
This is allowed. Not only is it allowed, it is the intention for

the code is even illformed

if /A/ makes /oskar/ public and /B/ uses public inheritance
then we can write
peter->oskar();

I think this is the OP's intent
 
B

Barry

Erik said:
As D. Susman pointed out, this does not look like what you asked for,
perhaps you meant something like this:

class A { public:
virtual void foo();
};

class B : public A {
void foo() { bar(); }; // Call B::bar() in B::foo()
void bar();
};

int main() {
A* a = new B;
a->foo(); // This will call B::bar();
}
 
J

Jonas Huckestein

<1190202346.278828.54350@o80g
2000hse.googlegroups.com> "D.
Susman"
Is there a typo here? This
does not demonstrate what
you're asking, Iguess.


Oh yeah, thanks :) It should
have been peter->oskar() :)

Regards,
Jonas
 
O

Old Wolf

perhaps you meant something like this:

class A {
virtual void foo();
};

class B : public A {
void foo() { bar(); }; // Call B::bar() in B::foo()
void bar();

};

int main() {
A* a = new B;
a->foo(); // This will call B::bar();
}

You need to make A::foo public.
 
E

Earl Purple

Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);

};

class B : A {
void emil(){};
void oskar(void)
{emil();};};

A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.

Regards,
Jonas

This would be allowed;

class A
{
public:
virtual void oskar(); // style issue: do not put void parameter in
but technically allowed

// A should also have a virtual destructor the way you use it thus:
virtual ~A() {}
};

class B : public A // note it must inherit public to use the
inheritance outside of class B itself
{
void emil(){};

void oskar() // it is allowed to be private here and will still
work
{
emil();
}
};

int main() // or any other function
{
A * peter = new B;
peter->oskar(); // allowed as long as oskar is public in A or this
function is a friend

delete peter; // and hope oskar() didn't throw, so preferably use
auto_ptr or scoped_ptr
// but we are illustrating access here
}

Another popular "style" issue is to make the virtual method protected
in A and have a public non-virtual method call it. (It is better to
make it protected rather than private, not because the derived class
can't override the private virtual, it can, and not because it "looks
confusing" (like the FAQ says) but because it cannot implement in
terms of its base class implementation).
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,651
Latest member
VeraPiw932

Latest Threads

Top