473,386 Members | 1,812 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,386 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 17444
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied...
8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
7
by: Simon Bond | last post by:
In C# we can go: <foo.cs> // Create a serializable class public class Foo : ISerializable { public void GetObjectData(SerializationInfo info, StreamingContext context) {};
1
by: Andreas Poller | last post by:
Hello, I have the following problem: I have a class deriving from ICustomTypeDescriptor: public __gc class TPropertyBag : public ICustomTypeDescriptor { private: ...
0
by: Primoz Gustincic | last post by:
I'm explicitly implementing an interface defined in assembly A. public __gc __interface IPop { Void GetPop(); }; public __abstract __gc class CPOP : public IPop { private:
8
by: julian_m | last post by:
I'm having problems with include. I wrote a small example which shows what's going on... I should say that the problems started after I moved to a shared server. All was working fine in my local...
1
by: Bruce | last post by:
public ref class GpsDevice abstract { /* Please note GarXface4::GpsDevice* is in a different namespace from this one and GarXface4::GpsDevice is an unmanaged class */ virtual...
2
by: Christopher | last post by:
If I have an generic interface, but _also_ have the implementation for it and want to use that common implementation in my derived classes, what method does that leave me to make pure virtual such...
1
by: jainchar | last post by:
hello i m in trouble that my project is in vc++ with mfc.my program giving the error that error c2259:"CException" cannot instantiate abstract class. when i remove this error then my...
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: 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:
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.