473,545 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual abstract functions

Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction () const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance

Oct 11 '05 #1
12 21207
* placid:

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error
No you don't.

If you try to compile the above you get some other error messages.

Here's an example of what you actually get, using g++ 3.4.2:

koko.cpp:6: error: `string' does not name a type
koko.cpp:6: error: extra semicolon
koko.cpp:13: error: new types may not be defined in a return type
koko.cpp:13: error: two or more data types in declaration of `main'
koko.cpp:13: error: extraneous `int' ignored
koko.cpp:13: error: `main' must return `int'
koko.cpp:13: error: return type for `main' changed to `int'
koko.cpp: In function `int main(...)':
koko.cpp:14: warning: unused variable 'aa'

Here's another example, when '#include <string>' and 'using namespace std;'
are added on the top:

koko.cpp:16: error: new types may not be defined in a return type
koko.cpp:16: error: two or more data types in declaration of `main'
koko.cpp:16: error: extraneous `int' ignored
koko.cpp:16: error: `main' must return `int'
koko.cpp:16: error: return type for `main' changed to `int'
koko.cpp: In function `int main(...)':
koko.cpp:17: error: cannot allocate an object of type `B'
koko.cpp:17: error: because the following virtual functions are abstract:
koko.cpp:9: error: virtual std::string A::someFunction () const
koko.cpp:17: warning: unused variable 'aa'

Now for the error message you _claim_ for the above code, but which is
actually for code that you haven't shown:

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction () const
Study the error message.

What does it say?

That's one thing that's wrong in your actual code.

does anyone know whats this abou, because if i remove the 'const' in
the Base class it works
No it does not.

but i wont the Base class to be abstract !


Why?

--
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?
Oct 11 '05 #2

placid wrote:
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction () const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance


Class B must implement someFunction for the class to be instantiable:

class B : public A
{
public:
virtual std::string someFunction() const
{
return "";
}
};

Note also the proper syntax to allocate a B object:

int main()
{
A* aa = new B;
...
}

Greg

Oct 11 '05 #3
"Greg" <gr****@pacbell .net> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com
placid wrote:

int main(void)
{
A* aa = new B();
}


Note also the proper syntax to allocate a B object:

int main()
{
A* aa = new B;
...
}

A* aa = new B();

is also correct. See section 5.3.4/15 of the standard. The use of () means
that the object is value initialized.

--
John Carson

Oct 11 '05 #4
John Carson wrote:
"Greg" <gr****@pacbell .net> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com
placid wrote:

int main(void)
{
A* aa = new B();
}


Note also the proper syntax to allocate a B object:

int main()
{
A* aa = new B;
...
}

A* aa = new B();

is also correct. See section 5.3.4/15 of the standard. The use of () means
that the object is value initialized.


Yes, the parentheses are legal in this new expression. But my point was
that the parentheses are completely superfluous when allocating an
instance of a class type. The object is default-initialized without the
parentheses, the object is default-initialized with the parentheses, so
their presence in the statement has no effect on its meaning.

The economy of expression principle holds that only those operators
needed to complete an expression should appear in it. In other words,
it is not a good idea to pepper one's code with aesthetically pleasing
operators that otherwise contribute nothing to its meaning. Doing so
makes the program harder to understand, since someone examining the
code must first decide whether an operator in a statement is performing
some necessary operation, or whether it can be excluded from analysis.

More importantly, superfluous operators in one context may suddenly
become meaningful in slightly different contexts and do so in
surprising ways. Even in a new expression, the Standard warns that the
presence of parentheses can have surprising effects (§5.3.4/3) - which
is another argument not to use parentheses indiscriminatel y in a C++
program, but to use them only on an as-needed basis.

Greg

Oct 11 '05 #5

"placid" <Bu****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction () const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance


virtual string someFunction() const = 0;

This is called "pure virtual". Pure virtual means there is no definition of
the method in the base class, and any class that is derived from the base
class MUST define this method. Your class B did not define this method.
Which is what the compile error is yelling about.

Either:
1. Don't make the method pure virtual so you dont' have to define it in the
derived class (remove the = 0 ) or
2. Define the method in your derived class B.
Oct 11 '05 #6

Greg wrote:
A* aa = new B;
...

A* aa = new B();
is also correct.

Yes, the parentheses are legal in this new expression. But... Doing so
makes the program harder to understand, since someone examining the
code must first decide whether an operator in a statement is performing
some necessary operation, or whether it can be excluded from analysis.

More importantly, superfluous operators in one context may suddenly
become meaningful in slightly different contexts and do so in
surprising ways. Even in a new expression, the Standard warns that the
presence of parentheses can have surprising effects (§5.3.4/3) - which
is another argument not to use parentheses indiscriminatel y in a C++
program, but to use them only on an as-needed basis.


are you saying that B::B() will not be called if I do new B; ?

Oct 11 '05 #7
* Greg:
John Carson wrote:
"Greg" <gr****@pacbell .net> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com
placid wrote:
>
> int main(void)
> {
> A* aa =3D new B();
> }
>

Note also the proper syntax to allocate a B object:

int main()
{
A* aa =3D new B;
...
}

A* aa =3D new B();

is also correct. See section 5.3.4/15 of the standard. The use of () means
that the object is value initialized.


Yes, the parentheses are legal in this new expression. But my point was
that the parentheses are completely superfluous when allocating an
instance of a class type. The object is default-initialized without the
parentheses, the object is default-initialized with the parentheses, so
their presence in the statement has no effect on its meaning.


In this particular case, yes. For a POD, no. The problem is that popular
compilers such as Microsoft's do not adhere to the standard in this respect.

--
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?
Oct 11 '05 #8

Jim Langston wrote:
"placid" <Bu****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction () const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance

virtual string someFunction() const = 0;

This is called "pure virtual". Pure virtual means there is no definition of
the method in the base class, and any class that is derived from the base
class MUST define this method. Your class B did not define this method.
Which is what the compile error is yelling about.


Neither point is completely accurate. There may or may not be a
definition of the pure virtual function in the base class. If the
program ever calls the pure virtual method in the base class
explicitly, then the pure virtual function must be defined.
Furthermore, a derived class is not obligated to define a pure virtual
method it inherits. A derived class can declare also declare the method
pure virtual and thereby become an abstract class like its base.
Either:
1. Don't make the method pure virtual so you dont' have to define it in the
derived class (remove the = 0 ) or
2. Define the method in your derived class B.


There is also a third option just mentioned: B declares the method pure
virtual itself.

Greg

Oct 11 '05 #9

Jim Langston wrote:
"placid" <Bu****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction () const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance

virtual string someFunction() const = 0;

This is called "pure virtual". Pure virtual means there is no definition of
the method in the base class, and any class that is derived from the base
class MUST define this method. Your class B did not define this method.
Which is what the compile error is yelling about.


yes i have already defined the pure virtual function in the derived
class but the compiler still complains about not being able to make a
A* aa = new B();

Either:
1. Don't make the method pure virtual so you dont' have to define it in the
derived class (remove the = 0 ) or
2. Define the method in your derived class B.


Oct 12 '05 #10

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

Similar topics

9
5010
by: richard.forrest1 | last post by:
I have a problem with an abstract interface class whose implementation classes need to return different iterator types (but with the same value_types etc). Classes A and B both conform to the same abstract Interface class. Interface has a pair of virtual functions begin() and end() that generate a typical STL style range. Classes A and B...
62
3318
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously bloats quickly for each new vtable needed. Execution slows down considerably as well. You can work around this by using interfaces referemnces which...
11
4334
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
37
4125
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
6
3129
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm little confused. Am I deleting the right object when I call the virtual deconstructor? I was under impression when creating a class if I'm not...
7
3077
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see item 7 for why this is virtual ...
4
1709
by: Arne Schmitz | last post by:
If i have an abstract base class, that only contains pure virtual methods (and maybe some non-virtual methods), is a vtable still being generated, for the first derived class that implements those pure virtuals? My idea is that as long as there are less than two virtual methods of the same name in existance, it would (in theory) not be...
14
4171
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
1763
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0; };
17
3511
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
7479
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...
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
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. ...
1
7439
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...
0
7773
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...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.