473,387 Members | 1,502 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,387 software developers and data experts.

problems with abstract classes

Hi,

I've a class Handler which contains a STL list
std::list<Abstract*> mAbstract;
which is storing elements of the abstract class Abstract.
Further this class has a getElement function to
return a specific element of the list.

The abstract class Abstract has some virtual pure function
which are used with all concrete classes derived from it.

One of the concrete classes derived from Abstract, let's
call it Concrete_A, has some public functions of specific purpose
and therefore not defined in the abstract class but just in
Concrete_A. Let's assume that one function is
double getTime();

In the future there should be some more concrete classes which
are used for other purposes. To have a "generic model" they
should be derived from class Abstract and added to the STL list
of the handler and finally be accessed by the Handler.

Now let's assume that I have another class Class_A which has a pointer
to the Handler class. Now, my idea is to retrieve the element from the
STL list using the handler and that call the function getTime:

Class_A *object = new Class_A;
double t = object->getHandler()->getElement(_ID_)->getTime();
// _ID_ is a specification of the element from list mAbstract

Doing that the compiler issues an error telling me that the function
getTime() is not known in class Objective.

I though it would be possible to use pointers to objects of the class
Abstract in the STL list of the class Handler and thus the compiler would
recognize at run-time that it's an object of class Concrete_A and call the
corresponding function getTime().
Is this approach feasible? If so, what am I doing wrong?
Thank you.

Chris

Jul 25 '05 #1
9 2282
Could following be done.....
(Here Class_test is the concrete derived from abstract)

Class_test *ct1 = new Class_test;
ct1 = object->getHandler()->getEleme*nt(_ID_);
double_t = ct1->getTime();

Jul 25 '05 #2
Could you provide the pseudo code.
You say that ur list contains of abstract class. And you have this
gettime function defined in derived class.
Now u r creating a handle to elements in abstract class list. When you
try to access it then you are using a base class pointer to a base
class member.
For virtual function to come into picture I presume your gettime will
be virtual in base class and handler will use base class pointer of
derived class object type, which I dont find in your statements above.
What do ya say. I think it will be more clear with pseudo code.

Jul 25 '05 #3
"Christian Christmann" <pl*****@yahoo.de> schrieb im Newsbeitrag
news:42***********************@newsread2.arcor-online.net...
Hi,

I've a class Handler which contains a STL list
std::list<Abstract*> mAbstract;
which is storing elements of the abstract class Abstract.
Further this class has a getElement function to
return a specific element of the list.

The abstract class Abstract has some virtual pure function
which are used with all concrete classes derived from it.

One of the concrete classes derived from Abstract, let's
call it Concrete_A, has some public functions of specific purpose
and therefore not defined in the abstract class but just in
Concrete_A. Let's assume that one function is
double getTime();

In the future there should be some more concrete classes which
are used for other purposes. To have a "generic model" they
should be derived from class Abstract and added to the STL list
of the handler and finally be accessed by the Handler.

Now let's assume that I have another class Class_A which has a pointer
to the Handler class. Now, my idea is to retrieve the element from the
STL list using the handler and that call the function getTime:

Class_A *object = new Class_A;
double t = object->getHandler()->getElement(_ID_)->getTime();
// _ID_ is a specification of the element from list mAbstract

Doing that the compiler issues an error telling me that the function
getTime() is not known in class Objective.

I though it would be possible to use pointers to objects of the class
Abstract in the STL list of the class Handler and thus the compiler would
recognize at run-time that it's an object of class Concrete_A and call the
corresponding function getTime().


How can the compiler recognize anything at run-time? The program might even
be running on a machine where no compiler has ever been installed. And what
were the compiler supposed to do if the object does not support the required
method?

Before you can call a member function of a derived class, that is not also a
(virtual) member of the base class, you have to test whether the pointer you
get from you list actually points to an instance of a derived class, that
supports that function. You have to break your statement into several parts:

Concret_A* derived =
dynamic_cast<Concret_A*>(object->getHandler->getElement(_ID_));
if (derived != 0)
{
t = derived->getTime();
}

dynamic_cast tests (at run-time) whether the object pointed to actually is
of the required type. If it is, it returns a pointer to that type, otherwise
it returns 0. Only if the pointer is non-zero you can access methods of the
now known type.

HTH
Heinz
Jul 25 '05 #4
On Mon, 25 Jul 2005 11:20:54 +0200, Christian Christmann
<pl*****@yahoo.de> wrote:
Hi,

I've a class Handler which contains a STL list
std::list<Abstract*> mAbstract;
which is storing elements of the abstract class Abstract.
Further this class has a getElement function to
return a specific element of the list.

The abstract class Abstract has some virtual pure function
which are used with all concrete classes derived from it.

One of the concrete classes derived from Abstract, let's
call it Concrete_A, has some public functions of specific purpose
and therefore not defined in the abstract class but just in
Concrete_A. Let's assume that one function is
double getTime();

In the future there should be some more concrete classes which
are used for other purposes. To have a "generic model" they
should be derived from class Abstract and added to the STL list
of the handler and finally be accessed by the Handler.

Now let's assume that I have another class Class_A which has a pointer
to the Handler class. Now, my idea is to retrieve the element from the
STL list using the handler and that call the function getTime:

Class_A *object = new Class_A;
double t = object->getHandler()->getElement(_ID_)->getTime();
// _ID_ is a specification of the element from list mAbstract

Doing that the compiler issues an error telling me that the function
getTime() is not known in class Objective.

I though it would be possible to use pointers to objects of the class
[...] thus the compiler would
recognize at run-time [...]


that's a contradicton per se. at run-time, the compiler has long done its
job. the compiler will not recognize anything at run-time.
it does not work like you imagine: in order to call a method declared in
the concrete derived class, you must have a pointer of this type. so you
need to type cast the pointer to your abstract base class in order to
access a method of the derived class which the latter did not inherit from
the base class.
however, i could imagine that this contradicts the purpose of your list of
pointers to the base class. still, with a pointer to your base class, you
can only access methods (at least) declared in this base class.
Jul 25 '05 #5
Probably this may answer ur query. I havent written handler.
#include <iostream>
#include <list>
using namespace std;

class abstract_class {
public:
virtual void printsomething() = 0;
virtual ~abstract_class(){};
};
class derived :public abstract_class {
public:
void printsomething() { cout << "In derived
printsomething" << endl; }
};
int main() {

std::list<abstract_class*> mylist;
abstract_class *x = new derived();
mylist.push_front(x);

list<abstract_class*>::iterator i;
for ( i=mylist.begin() ; i!=mylist.end() ; ++i) {
cout << *i << endl;
abstract_class* y = new derived();
y = *i;
y->printsomething();
}
}

Jul 25 '05 #6
In fact you can cut short following lines

abstract_class* y = new derived();
y = *i;
y->printsomething();

to

abstract_class* y
y = *i;
y->printsomething();

Jul 25 '05 #7
Concret_A* derived =
dynamic_cast<Concret_A*>(object->getHandler->getElement(_ID_));
if (derived != 0)
{
t = derived->getTime();
}
}
dynamic_cast tests (at run-time) whether the object pointed to actually is
of the required type. If it is, it returns a pointer to that type,
otherwise it returns 0. Only if the pointer is non-zero you can access
methods of the now known type.
Thank you. This was exactly what I was looking for.

HTH
Heinz

Best regards,
Chris

Jul 25 '05 #8
> that's a contradicton per se. at run-time, the compiler has long done its
job. the compiler will not recognize anything at run-time. it does not
work like you imagine: in order to call a method declared in the concrete
derived class, you must have a pointer of this type. so you need to type
cast the pointer to your abstract base class in order to access a method
of the derived class which the latter did not inherit from the base class.
however, i could imagine that this contradicts the purpose of your list of
pointers to the base class. still, with a pointer to your base class, you
can only access methods (at least) declared in this base class.

Thank you, dynamic casting was the solution. I used the approach
Heinz suggested in his post and it words fine.

Chris
Jul 25 '05 #9
> Could you provide the pseudo code.
You say that ur list contains of abstract class. And you have this gettime
function defined in derived class. Now u r creating a handle to elements
in abstract class list. When you try to access it then you are using a
base class pointer to a base class member.
For virtual function to come into picture I presume your gettime will be
virtual in base class and handler will use base class pointer of derived
class object type, which I dont find in your statements above. What do ya
say. I think it will be more clear with pseudo code.


Thank you for your answer.
I think the easiest way to solve the problem
is to use dynamic casting as Heinz suggested in his post. I tried it here
and it works.

Chris
Jul 25 '05 #10

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

Similar topics

13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
3
by: Sunny | last post by:
Hi again, in the past I have posted here a problem with static methods and abstract classes, and Jon Skeet and Richard Lowe have helped me to clarify the things. But now I have found another...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.