471,089 Members | 1,088 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Cannot instantiate abstract class

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

Sep 13 '05 #1
3 17370
* ernesto:
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);
C( A const& value )

private:
A mValue;
Needs to be changed to e.g.

A const* mValue;

but beware of object lifetime and object destruction responsibility.
};


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 13 '05 #2
ernesto wrote:
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.
Sep 13 '05 #3
We can create pointers as well as references to Abstract class , but
not objects.
Thanks
Bangalore

Sep 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Andreas Poller | last post: by
reply views Thread by Primoz Gustincic | last post: by
8 posts views Thread by julian_m | last post: by
2 posts views Thread by Christopher | last post: by

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.