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

abstract base class question

Is it possible to create an abstract base class without a pure virtual
function?

Thanks

Jack

Jul 9 '06 #1
14 1624
In article <11**********************@m73g2000cwd.googlegroups .com>,
ju******@gmail.com says...
Is it possible to create an abstract base class without a pure virtual
function?
It's entirely possible to create a class and _use_ it as an abstract
base class (i.e. you only ever derive from it, never instantiate it
directly) without a pure virtual function.

Using a pure virtual function enforces that intent -- i.e. the
compiler won't _let_ you instantiate a class that contains a pure
virtual function (or a derivative, unless all pure virtual functions
have been overridden).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 9 '06 #2
Thank a lot.
>
It's entirely possible to create a class and _use_ it as an abstract
base class (i.e. you only ever derive from it, never instantiate it
directly) without a pure virtual function.
So how to do it? Could you give an example?

Thanks

Jack

Jul 9 '06 #3
Jerry Coffin wrote:
In article <11**********************@m73g2000cwd.googlegroups .com>,
ju******@gmail.com says...
>Is it possible to create an abstract base class without a pure virtual
function?

It's entirely possible to create a class and _use_ it as an abstract
base class (i.e. you only ever derive from it, never instantiate it
directly) without a pure virtual function.

Using a pure virtual function enforces that intent -- i.e. the
compiler won't _let_ you instantiate a class that contains a pure
virtual function (or a derivative, unless all pure virtual functions
have been overridden).
I guess that OP could use a protected constructor or destructor, as well.
Jul 9 '06 #4
In article <11**********************@m79g2000cwm.googlegroups .com>,
ju******@gmail.com says...
Thank a lot.

It's entirely possible to create a class and _use_ it as an abstract
base class (i.e. you only ever derive from it, never instantiate it
directly) without a pure virtual function.

So how to do it? Could you give an example?
You write a class. You don't instantiate it. If you want an example
of a class that could be derived from:

class X {
virtual ~X() {}
};

If you intend something to be used as a base class, you typically
want it to have a virtual dtor. Anything else will usually be
specific to what that class is intended to do. I can't really give an
example of not instantiating it, because there's no code involved.
You write things like 'class Y : public X;', but never 'X x;' or
anything like that.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 9 '06 #5
red floyd wrote:
I guess that OP could use a protected constructor or destructor, as well.
That won't totally prevent instantiation:

class X {
protected;
X();
public:
static X* MakeX() { return new X; }
};

Jul 9 '06 #6
ju******@gmail.com wrote:
Is it possible to create an abstract base class without a pure virtual
function?
Well, there are way to make one that behaves similiar, but why would you
want to? You can always make the destructor pure if there is no other
member function that you could make pure.

Jul 9 '06 #7
Rolf Magnus wrote:
ju******@gmail.com wrote:
Is it possible to create an abstract base class without a pure virtual
function?

Well, there are way to make one that behaves similiar, but why would you
want to? You can always make the destructor pure if there is no other
member function that you could make pure.
Thats correct!
To elaborate more on Rolfs point,
A pure virtual function can have body but it can only be called from
the derived classes and not by user directly. So for example:

class A {
virtual void fun() = 0;
};

void a::fun() { _DO_SOMETHING_}

class B : public A {
void fun() {
A::fun(); // works fine
}
};

void main() {
A myA; // fails as the class is abstract..

B myB;
myB.fun(); // works fine
}

Now that said, If you dont want any function to be pure virtual it
means you intend to call them through derived class objects. In this
case you can make destructor pure virtual and provide it a body as well
because when ur class is extended (which it must since it is abstract)
it will be destroyed properly since destructor will always be called
through the derived class destructor which c++ compiler will allow.

Ramneek

Jul 9 '06 #8
On 9 Jul 2006 03:11:48 -0700, "rami" <on***********@gmail.comwrote
in comp.lang.c++:
Rolf Magnus wrote:
ju******@gmail.com wrote:
Is it possible to create an abstract base class without a pure virtual
function?
Well, there are way to make one that behaves similiar, but why would you
want to? You can always make the destructor pure if there is no other
member function that you could make pure.

Thats correct!
To elaborate more on Rolfs point,
A pure virtual function can have body but it can only be called from
the derived classes and not by user directly. So for example:

class A {
virtual void fun() = 0;
};

void a::fun() { _DO_SOMETHING_}

class B : public A {
void fun() {
A::fun(); // works fine
}
};

void main() {
You shouldn't post advice here until you become aware of the fact that
there is no such thing as "void main()" in C++. The standard requires
that main() be defined with a return type of int. "void main()" is
ill-formed, requiring a diagnostic. The behavior of any executable
generated is completely undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 9 '06 #9

Jack Klein wrote:
On 9 Jul 2006 03:11:48 -0700, "rami" <on***********@gmail.comwrote
in comp.lang.c++:
Rolf Magnus wrote:
ju******@gmail.com wrote:
>
Is it possible to create an abstract base class without a pure virtual
function?
>
Well, there are way to make one that behaves similiar, but why would you
want to? You can always make the destructor pure if there is no other
member function that you could make pure.
Thats correct!
To elaborate more on Rolfs point,
A pure virtual function can have body but it can only be called from
the derived classes and not by user directly. So for example:

class A {
virtual void fun() = 0;
};

void a::fun() { _DO_SOMETHING_}

class B : public A {
void fun() {
A::fun(); // works fine
}
};

void main() {

You shouldn't post advice here until you become aware of the fact that
there is no such thing as "void main()" in C++. The standard requires
that main() be defined with a return type of int. "void main()" is
ill-formed, requiring a diagnostic. The behavior of any executable
generated is completely undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
i think you shudnt be posting here till you spit that attitude out and
realize that this isnt my full time job and i am only trying to help
someone (It was a typo). Or is it that you dont have anything better to
post? Or is forum paying you for picking up garbage?

Jul 9 '06 #10
* rami:
Jack Klein wrote:
>On 9 Jul 2006 03:11:48 -0700, "rami" <on***********@gmail.comwrote
in comp.lang.c++:
>>Rolf Magnus wrote:
ju******@gmail.com wrote:

Is it possible to create an abstract base class without a pure virtual
function?
Well, there are way to make one that behaves similiar, but why would you
want to? You can always make the destructor pure if there is no other
member function that you could make pure.
Thats correct!
To elaborate more on Rolfs point,
A pure virtual function can have body but it can only be called from
the derived classes and not by user directly. So for example:

class A {
virtual void fun() = 0;
};

void a::fun() { _DO_SOMETHING_}

class B : public A {
void fun() {
A::fun(); // works fine
}
};

void main() {
You shouldn't post advice here until you become aware of the fact that
there is no such thing as "void main()" in C++. The standard requires
that main() be defined with a return type of int. "void main()" is
ill-formed, requiring a diagnostic. The behavior of any executable
generated is completely undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

i think you shudnt be posting here till you spit that attitude out and
realize that this isnt my full time job and i am only trying to help
someone (It was a typo). Or is it that you dont have anything better to
post? Or is forum paying you for picking up garbage?
Jack is right. Your posting contained a few errors and misleading text,
but formulated in such a way that many people would take it seriously
(they won't after your reply quoted above, but that's another matter).
Here are three content problems:

* You state "can only be called from the derived classes".
That is incorrect.

* You use "_DO_SOMETHING_" as if that is a valid macro name.
It isn't valid, at least not for your own macro. Names starting
with underscore followed by uppercase are reserved.

* You use "void main".
Already discussed by Jack.

So, please try to pick up a little about how this group works.

Nobody (at least, not I! ;-)) will yell at you for helping out with
facts that you know, or stating opinions, or giving advice in general;
if such postings were removed, then this group would fall from being a
high-traffic, useful group to being a dried-out very sporadic traffic
and mostly useless. It's when the advice is formulated as authoritative
but is in fact something you really don't know anything about, that it's
a problem. Because it might then mislead people and cause new "urban
legends" to be perpetuated, misleading even more people than just those
who originally read your posting and thought it was good.

Cheers,

- Alf

--
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?
Jul 9 '06 #11

Alf P. Steinbach wrote:
* rami:
Jack Klein wrote:
On 9 Jul 2006 03:11:48 -0700, "rami" <on***********@gmail.comwrote
in comp.lang.c++:

Rolf Magnus wrote:
ju******@gmail.com wrote:

Is it possible to create an abstract base class without a pure virtual
function?
Well, there are way to make one that behaves similiar, but why would you
want to? You can always make the destructor pure if there is no other
member function that you could make pure.
Thats correct!
To elaborate more on Rolfs point,
A pure virtual function can have body but it can only be called from
the derived classes and not by user directly. So for example:

class A {
virtual void fun() = 0;
};

void a::fun() { _DO_SOMETHING_}

class B : public A {
void fun() {
A::fun(); // works fine
}
};

void main() {
You shouldn't post advice here until you become aware of the fact that
there is no such thing as "void main()" in C++. The standard requires
that main() be defined with a return type of int. "void main()" is
ill-formed, requiring a diagnostic. The behavior of any executable
generated is completely undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
i think you shudnt be posting here till you spit that attitude out and
realize that this isnt my full time job and i am only trying to help
someone (It was a typo). Or is it that you dont have anything better to
post? Or is forum paying you for picking up garbage?

Jack is right. Your posting contained a few errors and misleading text,
but formulated in such a way that many people would take it seriously
(they won't after your reply quoted above, but that's another matter).
Here are three content problems:

* You state "can only be called from the derived classes".
That is incorrect.

* You use "_DO_SOMETHING_" as if that is a valid macro name.
It isn't valid, at least not for your own macro. Names starting
with underscore followed by uppercase are reserved.

* You use "void main".
Already discussed by Jack.

So, please try to pick up a little about how this group works.

Nobody (at least, not I! ;-)) will yell at you for helping out with
facts that you know, or stating opinions, or giving advice in general;
if such postings were removed, then this group would fall from being a
high-traffic, useful group to being a dried-out very sporadic traffic
and mostly useless. It's when the advice is formulated as authoritative
but is in fact something you really don't know anything about, that it's
a problem. Because it might then mislead people and cause new "urban
legends" to be perpetuated, misleading even more people than just those
who originally read your posting and thought it was good.

Cheers,

- Alf

--
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?
Thanks Alf,

I understand and support what you are saying and totally agree. But at
the same time i also discourage people sending such messages which
discourages other posters. If the same thing is said so that it makes
sense more than shows an attitude we would have more people helping
here.. He could have pointed out the typo's in the message and i would
have thanked him for it.. All i know is he wasted his, yours and my
energy + some google's webspace. The point he wanted to make could have
been made with a simple post after rectifying the errors.

P.S. Whats wrong here?
* You state "can only be called from the derived classes".
That is incorrect.
AFAIK, pure virtual functions with bodies can be invoked from the
derived classes only..

Ramneek

Jul 9 '06 #12
* rami:
>
P.S. Whats wrong here?
> * You state "can only be called from the derived classes".
That is incorrect.

AFAIK, pure virtual functions with bodies can be invoked from the
derived classes only..
Try this with your favorite compiler:

#include <iostream>
#include <ostream>

void say( char const s[] ) { std::cout << s << std::endl; }

struct Foo
{
virtual void bar() const = 0;
void g() const { Foo::bar(); }
};

void Foo::bar() const { say( "called" ); }

struct Foo2: Foo { void bar() const {} };

int main() { Foo2().g(); }
--
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?
Jul 9 '06 #13

Alf P. Steinbach wrote:
* rami:

P.S. Whats wrong here?
* You state "can only be called from the derived classes".
That is incorrect.
AFAIK, pure virtual functions with bodies can be invoked from the
derived classes only..

Try this with your favorite compiler:

#include <iostream>
#include <ostream>

void say( char const s[] ) { std::cout << s << std::endl; }

struct Foo
{
virtual void bar() const = 0;
void g() const { Foo::bar(); }
};

void Foo::bar() const { say( "called" ); }

struct Foo2: Foo { void bar() const {} };

int main() { Foo2().g(); }
--
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?
Thanks!

So is the point - "can only be called _through_ the derived class
objects"?

Ramneek

Jul 9 '06 #14
rami schrieb:
Alf P. Steinbach wrote:
>* rami:
>>AFAIK, pure virtual functions with bodies can be invoked from the
derived classes only..
Try this with your favorite compiler:

#include <iostream>
#include <ostream>

void say( char const s[] ) { std::cout << s << std::endl; }

struct Foo
{
virtual void bar() const = 0;
void g() const { Foo::bar(); }
};

void Foo::bar() const { say( "called" ); }

struct Foo2: Foo { void bar() const {} };

int main() { Foo2().g(); }
--
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?

Thanks!

So is the point - "can only be called _through_ the derived class
objects"?
Please don't quote signatures. Good newsreaders cut them automatically.

The point is that there is no restriction on how you can call pure
virtual functions.

However, you cannot instantiate a class with pure virtual functions (you
cannot have objects of this class).

--
Thomas
Jul 9 '06 #15

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

Similar topics

33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
4
by: Rachel Devons | last post by:
All, I'm struggling with an OOP concept. Let me try to define what I'm wanting by using some classic examples. Let's say that I have classes called Square & Circle that derive from class...
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
9
by: WithPit | last post by:
I am trying to create an Managed C++ Wrapper around an unmanaged library which contains C++ code. Some of the unmanaged methods returns an returntype which is of the abstract base type (for...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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.