polymorphic use problem

A

Angus

If I have these classes:


class TBase
{
public:
virtual int Execute(Server* srv) = 0;
virtual ~TFunction() {}
};

class Derived : public TBase
{
public:
Derived(const std::string& strA, int ia1)
: m_strA(strA), m_ia1(ia1){
}

virtual int Execute(TServer srv)
{
return SomeFunction(srv, m_strA, m_ia1);
}
~TRegister() { }
private:
std::string m_strA;
int m_ia1;
};


I want to call the Execute function on a Derived object. How can I
call it?
 
J

Joe Greer

If I have these classes:


class TBase
{
public:
virtual int Execute(Server* srv) = 0;
virtual ~TFunction() {}
};

class Derived : public TBase
{
public:
Derived(const std::string& strA, int ia1)
: m_strA(strA), m_ia1(ia1){
}

virtual int Execute(TServer srv)
{
return SomeFunction(srv, m_strA, m_ia1);
}
~TRegister() { }
private:
std::string m_strA;
int m_ia1;
};


I want to call the Execute function on a Derived object. How can I
call it?


Derived d("blah", 5);

TServer s;
d.Execute(s);

joe
 
J

Joe Greer

If I have these classes:


class TBase
{
public:
virtual int Execute(Server* srv) = 0;
virtual ~TFunction() {}
};

class Derived : public TBase
{
public:
Derived(const std::string& strA, int ia1)
: m_strA(strA), m_ia1(ia1){
}

virtual int Execute(TServer srv)
{
return SomeFunction(srv, m_strA, m_ia1);
}
~TRegister() { }
private:
std::string m_strA;
int m_ia1;
};


I want to call the Execute function on a Derived object. How can I
call it?

I have to point out that as written this won't compile because int
Execute(Server *) isn't implemented any where (unless TServer is a
typedef for a Server *). However, assuming you meant them to be the
same...

If you had:

void f(TBase & t)
{
Server s;
t.Execute(&s);
}
..
..
..

int main()
{
Derived d("blah", 4);

f(d);
}


Is that what you have in mind? Or was it more like:

void f(TBase * t)
{
Server s;
t->Execute(&s);
}

int main()
{
Derived d("blah", 3);
TBase * pT = &d;

f(pT);
}

Either way works. Polymorphism requires a pointer or reference at some
point or you aren't really polymorphic.

joe
 
R

red floyd

(e-mail address removed):








I have to point out that as written this won't compile because int
Execute(Server *) isn't implemented any where (unless TServer is a
typedef for a Server *).  However, assuming you meant them to be the
same...
I also have to point out that these won't compile because the
destructors are improperly named.
 

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
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top