473,379 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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?
Jul 10 '08 #1
4 1162
Angus <an*********@gmail.comwrote in news:67a176cb-5052-45d3-9dc4-
6b**********@27g2000hsf.googlegroups.com:
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
Jul 10 '08 #2
Angus <an*********@gmail.comwrote in news:67a176cb-5052-45d3-9dc4-
6b**********@27g2000hsf.googlegroups.com:
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
Jul 10 '08 #3
On Jul 10, 9:55*am, Joe Greer <jgr...@doubletake.comwrote:
Angus <anguscom...@gmail.comwrote in news:67a176cb-5052-45d3-9dc4-
6bc47f3ff...@27g2000hsf.googlegroups.com:
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...
I also have to point out that these won't compile because the
destructors are improperly named.
Jul 10 '08 #4
red floyd <re******@gmail.comwrote in news:11d9e89e-6410-44ad-9675-
db**********@m36g2000hse.googlegroups.com:
On Jul 10, 9:55*am, Joe Greer <jgr...@doubletake.comwrote:
>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
Jul 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Maurice Termeer | last post by:
Hi, suppose i've got this: class a { public: int n; }; class b : public a { public: };
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
1
by: verec | last post by:
Last week I asked here how I could detect that a T was polymorphic, and received very thoughtful and useful replies that I used straight away. Thanks to all who answered. This week, it turns...
7
by: Mr. Ed | last post by:
I have a base class which has about 150 derived classes. Most of the derived classes are very similar, and many don't change the base class at all. All the derived classes have a unique factory...
9
by: Karel Miklav | last post by:
In lots of places in a programm I need to identify type of received messages, so I create them as virtual classes and use RTTI to find their type later. But these are simple messages, often without...
7
by: James Fortune | last post by:
In response to different users or situations (data context) I transform the appearance and characteristics of Access Forms through code. This seems to fit in with the idea of polymorphism. Do...
1
by: Michal Piatkowski | last post by:
I have a problem with serialization of a polymorphic array. It has attributes defined XmlArray and XmlArrayItem for XML serialization. The attribute specifies Type1 but as my array is polymorphic...
12
by: Bob | last post by:
Hi, 'Shadowed' properties are not polymorphic. (See thread 'Inheritance and late binding') They should be. Problem: Base class has read only property 'X'. Derived class must have read / write...
4
by: Osamede.Zhang | last post by:
Compiler store a pointer table in each object with virtual fuction to implement polymorphic,isn't it? But where is the pointer table? It seems that object only store a pointer to the table,the...
7
by: Arindam | last post by:
#include <cstdio> struct Test { void bar() { foo(); } private: virtual void foo() { printf("Test\n"); }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.