Cannot instantiate abstract class

E

ernesto

Hi everybody:

I have the following implementations:

class A
{
public:
virtual int GetValue() = 0;
};

class B : public A
{
public:
B(int aValue);
virtual ~B();
virtual int GetValue();
private:
int mValue;
};

class C
{
public:
C(A aValue);

private:
A mValue;
};


My questions are: Why the compiler cannot instantiate abstract class on
my "class C" constructor implementation?

I want my class to work with instances of classes derived from A and it
does not allow to me do it.

Is there another way to do it?

Best regards,




ernesto
 
R

Rolf Magnus

ernesto said:
Hi everybody:

I have the following implementations:

class A
{
public:
virtual int GetValue() = 0;
};

class B : public A
{
public:
B(int aValue);
virtual ~B();
virtual int GetValue();
private:
int mValue;
};

class C
{
public:
C(A aValue);

private:
A mValue;
};


My questions are: Why the compiler cannot instantiate abstract class on
my "class C" constructor implementation?

I want my class to work with instances of classes derived from A and it
does not allow to me do it.

That's because C's constructor takes an A by value. That means it would copy
the object. But it can't do that, because A is abstract, and would actually
not be what you want. You need to pass by reference.

class C
{
public:
C(A& aValue);

private:
A& mValue;
};

PS: You don't seem to do const correct programming. I assume your
A::GetValue() member function doesn't change the value, which means that
this function should be 'virtual int GetValue() const = 0'. Similar for
class B.
 
B

Bangalore

We can create pointers as well as references to Abstract class , but
not objects.
Thanks
Bangalore
 

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,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top