LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2005, 05:19 PM   #1
Darwish
Member
 
Registered: Jun 2005
Location: Egypt (North Africa)
Distribution: Debian Sarge
Posts: 35

Rep: Reputation: 15
Unhappy Pure virtual function problem with G++


I have a problem when defining a pure virtual function in G++
here's a sample code:

virtual void print() = 0
{ cout << "Test" << endl; }

the code works without a glitch in VC++.., but when compiling under GCC it prints the error message:

error: Function definition with a pure virtual function..

Is pure virtual function definitions in G++ are prohibited?!!
and if it is prohibited is there another approach

Thanks for any help..
 
Old 07-06-2005, 12:09 AM   #2
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
I am using gcc version 3.3.5 (Debian 1:3.3.5-8ubuntu2). The following compiles cleanly for me:


Code:
#include <iostream>

class Foo
{
public:
	virtual void bar()=0;
};

void Foo::bar()
{
	std::cout << "Whee!\n";
}

class Baz : public Foo
{
public:
	virtual void bar() { std::cout << "Frotz!\n"; }
};

int main() 
{
	Foo* pfoo = new Baz();
	pfoo->bar();
	delete pfoo;

	return 0;
}
Output is "Frotz!".

Not sure what the syntax for declaring the body inline is

In case anyone is wondering, this example is somewhat nonsensical, as Foo::bar() can never be called because class Foo can never be instantiated. The body is somewhat meaningless. Scott Meyers demonstrates a use for a pure virtual function body in Item 36 of "Effective C++" (2nd Ed.).
 
Old 07-06-2005, 07:37 AM   #3
Darwish
Member
 
Registered: Jun 2005
Location: Egypt (North Africa)
Distribution: Debian Sarge
Posts: 35

Original Poster
Rep: Reputation: 15
Smile

Thanks alot..
I understood from ur code to define the pure virtual function outside the class and this method worked exactly as I need....

Last edited by Darwish; 07-06-2005 at 07:38 AM.
 
Old 07-08-2005, 07:04 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Also you can't define a pure virtual function; if you use "= 0" then you can't give it a definition, you can only declare it. Not only did carl.waldbieser put the function in the class definition, he also took out the "= 0".
ta0kira

PS Nevermind. Didn't look at it close enough the first time. Sorry!

Last edited by ta0kira; 07-08-2005 at 07:24 AM.
 
Old 07-08-2005, 08:19 PM   #5
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
I wasn't aware you could provide a body for a pure virtual function. I'm trying to visualize an example of using one, Carl I have the book "Effective C++: Second Edition", is this the book you're talking about? Besides the limited polymorphism I've used pure virtual functions for I have barely used them before. You guys might have inspired me to write some code tonite
 
Old 07-08-2005, 09:03 PM   #6
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Yes, that is the book I referred to. It is somewhat surprising and counterintuitive that the standard actually *does* allow you to define a body for a pure virtual function. Typically, pure virtual functions are used to define protocol classes (synonymous with "interfaces" in other languages), so the idea of having a body is somewhat of an oddity. Meyers demonstrates how you can use them to prevent pollution of a class namespace. I am not sure what kinds of practical applicability pure virtual function bodies have-- to me they seem about as obscure as the comma operator.
 
Old 07-09-2005, 08:06 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I can't see where the defined pure virtual function would ever get called unless you were to take a member pointer to it and explicitly call that, or otherwise fully qualify the function name when making the call. You can't even instantiate a derivative of the class without redefining it, thereby covering it up. Maybe this is how it would be used:
Code:
struct Base
{
	virtual
		void
	Function() = 0;
};

void Base::Function() {}

struct Derived :
	public Base
{
		void
	Function();
};

void Derived::Function()
{ Base::Function(); }

int main()
{
	//Base MyBase;
	Derived MyDerived;
}
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't enable virtual users on pure-ftpd G-Fox Linux - Networking 1 03-25-2010 04:51 PM
pure-ftpd and virtual servers mozilla Linux - Networking 1 05-03-2005 06:35 AM
pure ftp help virtual user creation Tinku Linux - General 2 10-10-2004 12:24 AM
Pure Virtual LuderForChrist Programming 3 07-07-2004 01:38 PM
pure-ftpd virtual users problem stofferthebest Linux - Networking 0 04-12-2004 03:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration