Copy a Base Class to a Derived Class

V

Vidar Hasfjord

[Frankenstein monster]

Absolutely :D I did more experiments and doesn't work. It was just an
experiment :)

Good. You'll sleep better. :)
[...]
So your technique doesn't solve any problems; and worse; it is not
reliable.

Ehi man, I never said "you folks out there doesn't know nothing about C
++, listen to me" :) I write in this list with the hope that all the
people that answer me have a very very deeper know-how about those
subject than me.

Sorry if I sounded too dismissive and condescending. It is an
interesting idea; although your attempted implementation does not work
in C++.
My "solution" is nothing, but I think that what I have in mind is not
so crazy.

Actually, the possibility of "upgrading" objects at runtime exists in
other more dynamic languages. In such languages you could easily
attach new information and methods to an object during program
execution.

So your idea is not crazy; just your experimental implementation in C+
+.

Regards,
Vidar Hasfjord
 
V

Vidar Hasfjord

Don't know about this pattern, I'll take a look.

The Proxy pattern can let you have the illusion of an "enhanced"
object the way you describe. But it doesn't change the object. This
distinction is very important.

For example, if you are given a QTcpSocket object by the Qt library in
some part of your code; say an event handler; you can temporarily
"enhance" that object while you process it.

But note that the extra information is not attached to the object, and
you have no way to access it through a base pointer to the same socket
object in some other part of the code. For that, you need an indirect
mechanism (hooks, if QTcpSocket supports that) or external mechanism
(e.g. map structure) as pointed out in my other reply.

That said, if you only require an enhanced view of the object
temporarily you can use the Proxy pattern. It requires that the object
has an abstract base though. For example:

class MyExtendedTcpSocket
: public QAbstractSocket
{
QTcpSocket* m_socket;
public:
MyExtendedTcpSocket (QTcpSocket* s)
: m_socket (s)
{}

// Implemenation
// Forwards calls to m_socket.

virtual qint64 bytesAvailable () const {
return m_socket->bytesAvailable ();
}

// ...

// Add your extensions here.
};

Now you can use this class whenever you are given a QTcpSocket, e.g.:

MyExtendedTcpSocket s = server.incomingConnection ();

You can pass your proxy to any function that operates on the abstract
base class QAbstractSocket. If you need to pass it to functions that
expects a QTcpSocket then simply add a conversion operator that
returns m_socket.

But note again; this techique does not modify or attach anything to
the original object. For example, it doesn not work for my event
handler example posted earlier; where you need to be able to access
the extra information via the base pointer.

Regards,
Vidar Hasfjord
 
L

LR

iakko said:
It's exactly what I want to do! Can you clarify this point please ?


I haven't run this through a compiler, and I think that others have
suggested similar. I suspect that we're having a communication problem
because I got the impression that you don't want to do something like
this, but here it is anyway.

class Base {
public:
void swap(Base &);
};

class Derived : public Base {
public:
void changeBase(const Base &b) {
Base temp(b);
Base::swap(temp);
}
};

There can be some tricky stuff here though if any of initialization of
Derived depends on anything in Base, we could easily end up with a
'broken' instance. As in this very contrived and very bad example:

class Base {
int j_;
public:
int *j() { return &j_; }
void swap(Base &b);
};

class Derived : public Base {
int *j;
public:
Derived(const Base &b) : j(b.j()) {}
void changeBase(const Base &b); // as above
};

int main() {
Base b1;
Base b2;
Derived d(b1);
d.changeBase(b2); // asking for trouble.
}

Thinking about it, maybe this would be better.

class Base {
public:
void swap(Base &b);
Base(const Base &);
};
class Derived : public Base {
public:
void swap(Derived &d) {
Base::swap(d);
// swap Derived members swapped here
}

Derived(const Base &b, const Derived &d)
:
Base(b),
// derived members initialized here
{}

void changeBase(const Base &b) {
Derived temp(b,*this);
swap(temp);
}
};




I think Bart v Ingen Schenau suggested a similar solution else thread
using assignment operators.

What Vidar Hasfjord said else thread about the ability to do this
existing in other more dynamic languages might be useful to think about.

LR
 
D

Daniel Pitts

iakko said:
No. That was an example showing that what Daniel Pitts said was not
the answer.


Well, it's an ugly workaround, because I'm going to reimplement all
the methods I need (maybe many), without reason. Usually, when you
want to reimplement a method from base class, it's due to the fact
that you need to add code inside for different behavior, in this case
it's only a call. This is against the inheritance purpose!
Have you considered using a delegation pattern, instead of inheritance?
Or, if you are lucky, Base may define a copy constructor, or operator=,
and just copy the value from your returned Base to the value of your new
Derived.
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top