473,405 Members | 2,415 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,405 software developers and data experts.

question about class instantiation

I have been exploring the concept of abstract classes and I was curious
- If I do not define a base class as abstract, will it be instantiated
(hope that is the right word) when a derived class is created?

if ( answer == false )
{
Would the idea of an abstract class simply be used to enforce integrity
of the classes by disallowing the abstract class to be instantiated, or
are there other purposes for it?
}
else if ( answer == true )
{
I'm guessing to prevent this would be the "why" and "when" of using
them, as well as enforce class integrity.
}

Is there a "proper" way of using/creating abstract classes?

Thanks

P.S. I apologize if this info is readily available on Google, but I did
a cursory search and came up with a lot of stuff that, while
interesting, did nothing to answer my very specific question. I know
that a vast quantity of the collected human intelligence on the subject
is here, so I thought I might go to the fount of knowledge directly.
Thanks again.
May 31 '07 #1
4 1882
Devon Null wrote:
I have been exploring the concept of abstract classes and I was
curious - If I do not define a base class as abstract, will it be
instantiated (hope that is the right word) when a derived class is
created?
Any base class subobject is instantiated as part of the derived
class object. The "abstract" trait prohibits from stand-alone
instantiation of the object.
>
if ( answer == false )
{
Would the idea of an abstract class simply be used to enforce
integrity of the classes by disallowing the abstract class to be
instantiated, or are there other purposes for it?
The class' being abstract means it doesn't have the behaviour
completely defined, which means that a stand-alone object of
that type cannot exist since it cannot _behave_, in some special
cases.

Prohibiting instantiation of an abstract class is a preventative
measure. The compiler doesn't have to guess whether this class
is ever used in a way that would have undefined behaviour. Here
is an example of what _might_ be OK:

struct Abstract {
virtual void foo() = 0;
};

int main() {
Abstract a;
}

In that program 'foo' behaviour is never invoked, which should
be OK, since there is no undefined behaviour anywhere. But
making the compiler analyze all possible situations and allow
the ones like this, but disallow others, would be too complex.
That's why the Standard simply disallows creation of 'Abstract'
object.
}
else if ( answer == true )
{
I'm guessing to prevent this would be the "why" and "when" of using
them, as well as enforce class integrity.
}

Is there a "proper" way of using/creating abstract classes?
The only way to "properly" use abstract classes is to have them
as arguments passed by reference or pointer, and instantiate them
ONLY as base class subobjects.
>
Thanks

P.S. I apologize if this info is readily available on Google, but I
did a cursory search and came up with a lot of stuff that, while
interesting, did nothing to answer my very specific question. I know
that a vast quantity of the collected human intelligence on the
subject is here, so I thought I might go to the fount of knowledge
directly. Thanks again.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 31 '07 #2
Victor Bazarov wrote:
Devon Null wrote:
>I have been exploring the concept of abstract classes and I was
curious - If I do not define a base class as abstract, will it be
instantiated (hope that is the right word) when a derived class is
created?

Any base class sub-object is instantiated as part of the derived
class object. The "abstract" trait prohibits from stand-alone
instantiation of the object.
Let's say I had class bar which is derived from abstract class foo,
would both classes take memory:

((total mem usage == (abstract foo) + bar) < (total mem usage ==
(non-abstract foo) + bar))

or would the mem requirements still be the same regardless if foo was
abstract:

((total mem usage == (abstract foo) + bar) == (total mem usage ==
(non-abstract foo) + bar))
>if ( answer == false )
{
Would the idea of an abstract class simply be used to enforce
integrity of the classes by disallowing the abstract class to be
instantiated, or are there other purposes for it?

The class' being abstract means it doesn't have the behaviour
completely defined, which means that a stand-alone object of
that type cannot exist since it cannot _behave_, in some special
cases.

Prohibiting instantiation of an abstract class is a preventative
measure. The compiler doesn't have to guess whether this class
is ever used in a way that would have undefined behaviour. Here
is an example of what _might_ be OK:

struct Abstract {
virtual void foo() = 0;
};

int main() {
Abstract a;
}

In that program 'foo' behaviour is never invoked, which should
be OK, since there is no undefined behaviour anywhere. But
making the compiler analyze all possible situations and allow
the ones like this, but disallow others, would be too complex.
That's why the Standard simply disallows creation of 'Abstract'
object.
>}
else if ( answer == true )
{
I'm guessing to prevent this would be the "why" and "when" of using
them, as well as enforce class integrity.
}

Is there a "proper" way of using/creating abstract classes?

The only way to "properly" use abstract classes is to have them
as arguments passed by reference or pointer, and instantiate them
ONLY as base class sub-objects.
Pardon any ignorance, but could you please explain/clarify this
statement? I do get that part about it only being a base class. Kinda
useless for anything else if you can't instantiate it.
>Thanks

P.S. I apologize if this info is readily available on Google, but I
did a cursory search and came up with a lot of stuff that, while
interesting, did nothing to answer my very specific question. I know
that a vast quantity of the collected human intelligence on the
subject is here, so I thought I might go to the fount of knowledge
directly. Thanks again.

V

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
May 31 '07 #3
Devon Null wrote:
Victor Bazarov wrote:
>Devon Null wrote:
>>I have been exploring the concept of abstract classes and I was
curious - If I do not define a base class as abstract, will it be
instantiated (hope that is the right word) when a derived class is
created?

Any base class sub-object is instantiated as part of the derived
class object. The "abstract" trait prohibits from stand-alone
instantiation of the object.

Let's say I had class bar which is derived from abstract class foo,
would both classes take memory:

((total mem usage == (abstract foo) + bar) < (total mem usage ==
(non-abstract foo) + bar))

or would the mem requirements still be the same regardless if foo was
abstract:

((total mem usage == (abstract foo) + bar) == (total mem usage ==
(non-abstract foo) + bar))
The amount of memory taken up by an instance of the class has nothing
to do with its abstractness. The size is calculated from the sizes
of base class subobjects, sizes of data members, additional padding
and other implementation-defined things (like virtual function table
pointer, for example).
>>[..]
Is there a "proper" way of using/creating abstract classes?

The only way to "properly" use abstract classes is to have them
as arguments passed by reference or pointer, and instantiate them
ONLY as base class sub-objects.

Pardon any ignorance, but could you please explain/clarify this
statement? I do get that part about it only being a base class. Kinda
useless for anything else if you can't instantiate it.
I am not sure what clarification you need, but I'll try.

struct Abstract {
virtual void foo() = 0;
};

void func(Abstract& a) {
a.foo(); // you dn't know whether it's a stand-alone instance
}

void func(Abstract* pa) {
pa->foo();
}
//////////////////////////////////////
struct Derived : Abstract {
void foo() {}
};

int main() {
Derived d;
foo(d);
foo(&d);
}

Now, the portion before the comment is where the abstract class is
used. The only way to use an instance of it is in a function where
the instance (the act of its creation aside for a moment) is passed
by reference or by pointer.

In the portion after the comment - the only way to instantiate the
abstract class, as a base class to non-abstract class. In the
'main' function 'd' is an instance of a concrete type.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 31 '07 #4
Victor Bazarov wrote:
>>>[..]
Is there a "proper" way of using/creating abstract classes?
The only way to "properly" use abstract classes is to have them
as arguments passed by reference or pointer, and instantiate them
ONLY as base class sub-objects.
Pardon any ignorance, but could you please explain/clarify this
statement? I do get that part about it only being a base class. Kinda
useless for anything else if you can't instantiate it.

I am not sure what clarification you need, but I'll try.

struct Abstract {
virtual void foo() = 0;
};

void func(Abstract& a) {
a.foo(); // you don't know whether it's a stand-alone instance
}

void func(Abstract* pa) {
pa->foo();
}
//////////////////////////////////////
struct Derived : Abstract {
void foo() {}
};

int main() {
Derived d;
foo(d);
foo(&d);
}

Now, the portion before the comment is where the abstract class is
used. The only way to use an instance of it is in a function where
the instance (the act of its creation aside for a moment) is passed
by reference or by pointer.

In the portion after the comment - the only way to instantiate the
abstract class, as a base class to non-abstract class. In the
'main' function 'd' is an instance of a concrete type.

V
The clarification was in the wording. It was the line "The only way to
"properly" use abstract classes is to have them as arguments passed by
reference or pointer." I thought this (above) was what you meant, but
for some reason my brain was having trouble piecing it together. Thanks
for all your help.

DN

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
Jun 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
5
by: Brian | last post by:
I am "learning" C# and have run into a problem that, though I can work around it, I would like to know what the *right* way to handle the issue is. I have created an "Info" struct and assigned...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
29
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.