Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 10th, 2008, 05:35 PM
Angus
Guest
 
Posts: n/a
Default polymorphic use problem

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?
  #2  
Old July 10th, 2008, 05:45 PM
Joe Greer
Guest
 
Posts: n/a
Default Re: polymorphic use problem

Angus <anguscomber@gmail.comwrote in news:67a176cb-5052-45d3-9dc4-
6bc47f3fff1f@27g2000hsf.googlegroups.com:
Quote:
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
  #3  
Old July 10th, 2008, 06:05 PM
Joe Greer
Guest
 
Posts: n/a
Default Re: polymorphic use problem

Angus <anguscomber@gmail.comwrote in news:67a176cb-5052-45d3-9dc4-
6bc47f3fff1f@27g2000hsf.googlegroups.com:
Quote:
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
  #4  
Old July 10th, 2008, 06:45 PM
red floyd
Guest
 
Posts: n/a
Default Re: polymorphic use problem

On Jul 10, 9:55*am, Joe Greer <jgr...@doubletake.comwrote:
Quote:
Angus <anguscom...@gmail.comwrote in news:67a176cb-5052-45d3-9dc4-
6bc47f3ff...@27g2000hsf.googlegroups.com:
>
>
>
Quote:
If I have these classes:
>
Quote:
class TBase
{
public:
* * virtual int Execute(Server* srv) = 0;
* * virtual ~TFunction() {}
};
>
Quote:
class Derived : public TBase
{
public:
* * Derived(const std::string& strA, int ia1)
* * : m_strA(strA), m_ia1(ia1){
* * }
>
Quote:
* * virtual int Execute(TServer srv)
* * {
* * * * return SomeFunction(srv, m_strA, m_ia1);
* * }
* * ~TRegister() { }
private:
* * std::string m_strA;
* * int m_ia1;
};
>
Quote:
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...
>
I also have to point out that these won't compile because the
destructors are improperly named.
  #5  
Old July 10th, 2008, 06:55 PM
Joe Greer
Guest
 
Posts: n/a
Default Re: polymorphic use problem

red floyd <redfloyd@gmail.comwrote in news:11d9e89e-6410-44ad-9675-
db3db1f230aa@m36g2000hse.googlegroups.com:
Quote:
On Jul 10, 9:55*am, Joe Greer <jgr...@doubletake.comwrote:
Quote:
>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.
>
There is that too. :)

joe
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.