How to override virtual method of inner class?

R

Ralf

Problem: Overriding virtual method of inner class of base, in a derived class.
The following gives compilation error.
How to solve this problem?

class TCB
{
public:
TCB() { }

struct TS
{
void Print() { /*...*/ ; PrintMore(); }
virtual void PrintMore() { }
} S;
};

class TCD : virtual public TCB
{
public:
TCD() { }
void TS::printMore() { /*...*/ } // test.cpp:19: error: cannot define member function ‘TCB::TS::printMore’ within ‘TCD’
};

int main(int argc, char* argv[])
{
TCD O;
O.S.Print();
return 0;
}
 
R

Ralf

blargg said:
Ralf said:
Problem: Overriding virtual method of inner class of base, in a derived
class.
The following gives compilation error.
How to solve this problem?

class TCB
{
public:
TCB() { }

struct TS
{
void Print() { /*...*/ ; PrintMore(); }
virtual void PrintMore() { }
} S;
};

class TCD : virtual public TCB
{
public:
TCD() { }
void TS::printMore() { /*...*/ } // test.cpp:19: error: cannot define
member function ŒTCB::TS::printMore¹ within ŒTCD¹
};

int main(int argc, char* argv[])
{
TCD O;
O.S.Print();
return 0;
}

An inner class is a class whose name is merely nested within the
enclosing class. Your example above is little different than (sorry, had
to change the names; much clearer this way)

struct X { virtual void f(); };

struct B { X x; };

struct D : virtual B
{
// B has no virtual functions to override
};


Sorry, I don't understand what you mean.
The question is:
How can I override the virtual method PrintMore() of the inner class?
The derived class is just an helper, but it doesn't work.
My intuition says that what I want should be possible somehow, but how?
 
S

Steve Wolter

blargg said:
Ralf said:
Problem: Overriding virtual method of inner class of base, in a derived
class.
The following gives compilation error.
How to solve this problem?

class TCB
{
public:
TCB() { }

struct TS
{
void Print() { /*...*/ ; PrintMore(); }
virtual void PrintMore() { }
} S;
};

class TCD : virtual public TCB
{
public:
TCD() { }
void TS::printMore() { /*...*/ } // test.cpp:19: error: cannot define
member function ¼TCB::TS::printMore¹ within ¼TCD¹
};

int main(int argc, char* argv[])
{
TCD O;
O.S.Print();
return 0;
}

An inner class is a class whose name is merely nested within the
enclosing class. Your example above is little different than (sorry, had
to change the names; much clearer this way)

struct X { virtual void f(); };

struct B { X x; };

struct D : virtual B
{
// B has no virtual functions to override
};


Sorry, I don't understand what you mean.
The question is:
How can I override the virtual method PrintMore() of the inner class?
The derived class is just an helper, but it doesn't work.
My intuition says that what I want should be possible somehow, but how?

It certainly is, and blargg had the right answer: The only connection
an inner class has to the outer is the namespace, thus you must derive
the overriding class from the inner class. So,

class TCD : virtual public TCB::TS

should do the trick.

Regards, Steve
 

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,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top