473,725 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17459
* 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
2794
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 into this property in its Load event. So, I gave the BaseForm and the property 'abstract' modifier, and put the implementation of the property in the inherited class; say MyForm. However, when I did this, I no longer can open MyForm in the design...
8
21722
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. Dev
7
10567
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
2829
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
966
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
11249
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 server... file test.php --------------------------------------------------- <?php
1
1518
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 GarXface4::GpsDevice* GetDevice() = 0; };
2
1470
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 that my generic interface class cannot be instantiated? Do you make the destructor virtual? Do you move constructors to private? // What do I make pure virtual?? AbstractClass
1
6434
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 project is giving the error in file afxres.rc .according to the error when i remove it and compile or debug the project then this can't save.so what should i do.please help me and can somebody give me the guideline that what is the afxres.rc file.
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.