Function expecting reference to object of Abstract class/structure

D

dust

I have a class/stucture A which is abstract as it contains all virtual
member functions. Now I have to call a function(void func(A& client),
but this function is expecting reference to object of type A. Since A
is abstract, I can't create an object, so how can I send an object to
this function? or how do deal with such situation.

-Dust
 
J

Jon

dust said:
I have a class/stucture A which is abstract as it contains all virtual
member functions.

Virtual functions don't make a class abstract. *Pure* virtual functions
do.
Now I have to call a function(void func(A& client),
but this function is expecting reference to object of type A. Since A
is abstract, I can't create an object, so how can I send an object to
this function? or how do deal with such situation.

It's not really "a situation", it's that you don't understand the
concepts involved. Research the topic "abstract vs. non-abstract" class.
(Some of the younger set like to use "concrete" to mean "non-abstract",
but for those who learned C++ around the time it was invented, "concrete"
has a very different meaning.)

class A // A is abstract
{
public:
virtual void DoSomething() = 0; // pure virtual function
};

class B: public A // B is not abstract
{
public:
// 'virtual' keyword is optional: compiler will make it virtual
virtual void DoSomething(){} // provided implementation
};

void func(A& client)
{
client.DoSomething(); // function from class derived from A will be
called
}

void func2()
{
B b;
func(b); // C++ polymorphism machinery makes this work
}
 
R

red floyd

Virtual functions don't make a class abstract. *Pure* virtual functions
do.
OP had specified *pure* virtual function. You redacted the word pure
in
your cut&paste.


You don't actually pass an A to that function, you pass an object of
some
class publicly derived from A. What func() is really saying is, I
need a
reference to an object that implements A's public interface.
 
D

dust

OP had specified *pure* virtual function.  You redacted the word pure
in
your cut&paste.

Yes, I have written virtual in first post, but deleted that and posted
new with added 'pure'. Jon is true and you too ;)
 
F

Fred Zwarts

dust said:
Yes, I have written virtual in first post, but deleted that and posted
new with added 'pure'. Jon is true and you too ;)

And as deleting posts does not work,
I see both posts in the newsgroup.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top