473,795 Members | 3,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need clarification about virtual methods & inheritance

I have this class:
class Selections {
OSStatus Init();

protected:
CFMutableSetRef selectionObject s;
static void CFASelectionsAp plier(const void* value, void* ctx);
OSType ready;
public:
Selections();
Selections(cons t Selections &obj);
virtual ~Selections();

virtual OSType GetSelectionSco pe();

virtual UInt32 GetSelectCount( SelectionFlag whichFlags);
virtual CFArrayRef GetSelObjects(S electionFlag whichFlags);
UInt32 GetCountOfClass (SelObjectClass theClass);
CFArrayRef GetSelObjectsOf Class(SelObject Class theClass);
virtual UInt32 GetCountOfScope (OSType theScope);
virtual CFArrayRef GetArrayofScope (OSType theScope);

virtual OSStatus MassAddSelObjec ts(CFArrayRef sorArray);
virtual OSStatus AddSelectededOb ject(SelectedOb jectRef soref);
virtual OSStatus MassRemoveSelOb jects(CFArrayRe f sorArray);
virtual OSStatus RemoveSelectedO bject(SelectedO bjectRef soref);
virtual void RemoveAllSelect edObjects(Selec tionFlag whichFlags);
void RemoveSelObject sOfClass(SelObj ectClass theClass);
virtual void RemoveSelObject sOfScope(OSType theScope);
virtual void RemoveAll();
};

and a subclass:

typedef class DirView DirView;

class DVSelections: public Selections {
DirView* view;
OSType dvsReady;
public:
DVSelections(Di rView* theView);
DVSelections(co nst DVSelections &obj);
~DVSelections() ;

OSType GetSelectionSco pe();
DirView* GetView();
};

When I attempt to compile it, the linker complains with this message:
/usr/bin/ld: Undefined symbols:
vtable for DVSelections

I was under the impression if the virtual function is not defined in a
subclass, the parent class's function will be used.
What am I missing?

This is mac OS X, XCode 2.2 with gcc 4.

TIA
Mark
Jan 13 '06 #1
10 2272
mark wrote:
I have this class:
class Selections {
OSStatus Init();

protected:
CFMutableSetRef selectionObject s;
static void CFASelectionsAp plier(const void* value, void* ctx);
OSType ready;
public:
Selections();
Selections(cons t Selections &obj);
virtual ~Selections();

virtual OSType GetSelectionSco pe();

virtual UInt32 GetSelectCount( SelectionFlag whichFlags);
virtual CFArrayRef GetSelObjects(S electionFlag whichFlags);
UInt32 GetCountOfClass (SelObjectClass theClass);
CFArrayRef GetSelObjectsOf Class(SelObject Class theClass);
virtual UInt32 GetCountOfScope (OSType theScope);
virtual CFArrayRef GetArrayofScope (OSType theScope);

virtual OSStatus MassAddSelObjec ts(CFArrayRef sorArray);
virtual OSStatus AddSelectededOb ject(SelectedOb jectRef soref);
virtual OSStatus MassRemoveSelOb jects(CFArrayRe f sorArray);
virtual OSStatus RemoveSelectedO bject(SelectedO bjectRef soref);
virtual void RemoveAllSelect edObjects(Selec tionFlag whichFlags);
void RemoveSelObject sOfClass(SelObj ectClass theClass);
virtual void RemoveSelObject sOfScope(OSType theScope);
virtual void RemoveAll();
};

and a subclass:

typedef class DirView DirView;

class DVSelections: public Selections {
DirView* view;
OSType dvsReady;
public:
DVSelections(Di rView* theView);
DVSelections(co nst DVSelections &obj);
~DVSelections() ;

OSType GetSelectionSco pe();
DirView* GetView();
};

When I attempt to compile it, the linker complains with this message:
/usr/bin/ld: Undefined symbols:
vtable for DVSelections

I was under the impression if the virtual function is not defined in a
subclass, the parent class's function will be used.
What am I missing?


Could it be you're not linking all object modules together when you need to?
Also, remember that all virtual functions that are not declared pure need
to be defined. Did you define any of them or did you just declare them?

If none of that helps, we probably can't really help you, since (a) you
didn't post the whole code, and (b) your issue is compiler-specific,
apparently, so you might want to consider posting to 'gnu.g++.help'.

V
Jan 13 '06 #2
On Fri, 13 Jan 2006 14:34:38 +1300 in comp.lang.c++, mark
<tr*****@ihug.c o.nz> wrote,
I was under the impression if the virtual function is not defined in a
subclass, the parent class's function will be used.


Yes, however if you declare it in the subclass declaration, that
means you are promising to define it somewhere.

Jan 13 '06 #3
In article <Hq************ ********@comcas t.com>,
"Victor Bazarov" <v.********@com Acast.net> wrote:
mark wrote:
I have this class:
class Selections {
OSStatus Init();

protected:
CFMutableSetRef selectionObject s;
static void CFASelectionsAp plier(const void* value, void* ctx);
OSType ready;
public:
Selections();
Selections(cons t Selections &obj);
virtual ~Selections();

virtual OSType GetSelectionSco pe();

virtual UInt32 GetSelectCount( SelectionFlag whichFlags);
virtual CFArrayRef GetSelObjects(S electionFlag whichFlags);
UInt32 GetCountOfClass (SelObjectClass theClass);
CFArrayRef GetSelObjectsOf Class(SelObject Class theClass);
virtual UInt32 GetCountOfScope (OSType theScope);
virtual CFArrayRef GetArrayofScope (OSType theScope);

virtual OSStatus MassAddSelObjec ts(CFArrayRef sorArray);
virtual OSStatus AddSelectededOb ject(SelectedOb jectRef soref);
virtual OSStatus MassRemoveSelOb jects(CFArrayRe f sorArray);
virtual OSStatus RemoveSelectedO bject(SelectedO bjectRef soref);
virtual void RemoveAllSelect edObjects(Selec tionFlag whichFlags);
void RemoveSelObject sOfClass(SelObj ectClass theClass);
virtual void RemoveSelObject sOfScope(OSType theScope);
virtual void RemoveAll();
};

and a subclass:

typedef class DirView DirView;

class DVSelections: public Selections {
DirView* view;
OSType dvsReady;
public:
DVSelections(Di rView* theView);
DVSelections(co nst DVSelections &obj);
~DVSelections() ;

OSType GetSelectionSco pe();
DirView* GetView();
};

When I attempt to compile it, the linker complains with this message:
/usr/bin/ld: Undefined symbols:
vtable for DVSelections

I was under the impression if the virtual function is not defined in a
subclass, the parent class's function will be used.
What am I missing?


Could it be you're not linking all object modules together when you need to?
Also, remember that all virtual functions that are not declared pure need
to be defined. Did you define any of them or did you just declare them?

If none of that helps, we probably can't really help you, since (a) you
didn't post the whole code, and (b) your issue is compiler-specific,
apparently, so you might want to consider posting to 'gnu.g++.help'.

V


All virtual functions for the base class have been declared and coded.

There is a function with this code:

OSStatus CreateNewDVSele ction(DirView* theView) {
if (theView==nil) {return paramErr;}
DVSelectionsRef dvs=nil;

try {
(1) dvs=new DVSelections(th eView);
}
catch (OSStatus OE) {
if (dvs) {delete dvs;}
return OE;
}
catch (...) {
if (dvs) {delete dvs;}
return memFullErr;
};

return 0;
};

Could (1) be causing the problem?
Jan 13 '06 #4
mark wrote:
In article <Hq************ ********@comcas t.com>,
"Victor Bazarov" <v.********@com Acast.net> wrote:
mark wrote:
I have this class:
[..]
I was under the impression if the virtual function is not defined
Virtual functions have to be either declared *pure* or defined (or both).
You cannot have a virtual function not defined, and not pure.
in a subclass, the parent class's function will be used.
What am I missing?

That's what you're missing. You need to define all functions that are
not pure.
Could it be you're not linking all object modules together when you
need to? Also, remember that all virtual functions that are not
declared pure need to be defined. Did you define any of them or did
you just declare them?

If none of that helps, we probably can't really help you, since (a)
you didn't post the whole code, and (b) your issue is
compiler-specific, apparently, so you might want to consider posting
to 'gnu.g++.help'.

V
All virtual functions for the base class have been declared and coded.


What do you mean by "coded"? Defined? Are you linking the module where
they are defined into your program?

There is a function with this code:

OSStatus CreateNewDVSele ction(DirView* theView) {
if (theView==nil) {return paramErr;}
What is 'nil'?
DVSelectionsRef dvs=nil;
What is 'nil'? What is 'DVSelectionsRe f'?
try {
(1) dvs=new DVSelections(th eView);
}
catch (OSStatus OE) {
if (dvs) {delete dvs;}
return OE;
}
catch (...) {
if (dvs) {delete dvs;}
return memFullErr;
};

return 0;
};

Could (1) be causing the problem?


No. The code itself if fine. The fact that you didn't define all
the virtual functions, or didn't provide the definitions to the linker,
is the problem.

V
Jan 13 '06 #5
In article <BM************ ********@comcas t.com>,
"Victor Bazarov" <v.********@com Acast.net> wrote:
mark wrote:
In article <Hq************ ********@comcas t.com>,
"Victor Bazarov" <v.********@com Acast.net> wrote:
mark wrote:
I have this class:
[..]
I was under the impression if the virtual function is not defined
Virtual functions have to be either declared *pure* or defined (or both).
You cannot have a virtual function not defined, and not pure.
in a subclass, the parent class's function will be used.
What am I missing?

That's what you're missing. You need to define all functions that are
not pure.

Thanks. Now I get it.

What is 'nil'?

Old pascal name for NULL.
Jan 13 '06 #6
In article <44************ ****@news.west. earthlink.net>,
David Harmon <so****@netcom. com> wrote:
On Fri, 13 Jan 2006 14:34:38 +1300 in comp.lang.c++, mark
<tr*****@ihug.c o.nz> wrote,
I was under the impression if the virtual function is not defined in a
subclass, the parent class's function will be used.


Yes, however if you declare it in the subclass declaration, that
means you are promising to define it somewhere.


I didn't define the virtual in the subclass, only the base class (and
it's not pure).
Jan 13 '06 #7

mark wrote:
In article <44************ ****@news.west. earthlink.net>,
David Harmon <so****@netcom. com> wrote:
On Fri, 13 Jan 2006 14:34:38 +1300 in comp.lang.c++, mark
<tr*****@ihug.c o.nz> wrote,
I was under the impression if the virtual function is not defined in a
subclass, the parent class's function will be used.


Yes, however if you declare it in the subclass declaration, that
means you are promising to define it somewhere.


I didn't define the virtual in the subclass, only the base class (and
it's not pure).


If a function is declared virtual in a base class, it is also virtual
in every derived class whether or not the derived class uses the
"virtual" keyword in its declaration of the function.

I suspect that you have not defined the (virtual) destructor for
DVSelections anywhere.

Greg

Jan 13 '06 #8
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
"Greg" <gr****@pacbell .net> wrote:
mark wrote:
In article <44************ ****@news.west. earthlink.net>,
David Harmon <so****@netcom. com> wrote:
On Fri, 13 Jan 2006 14:34:38 +1300 in comp.lang.c++, mark
<tr*****@ihug.c o.nz> wrote,
>I was under the impression if the virtual function is not defined in a
>subclass, the parent class's function will be used.

Yes, however if you declare it in the subclass declaration, that
means you are promising to define it somewhere.

I didn't define the virtual in the subclass, only the base class (and
it's not pure).


If a function is declared virtual in a base class, it is also virtual
in every derived class whether or not the derived class uses the
"virtual" keyword in its declaration of the function.

I suspect that you have not defined the (virtual) destructor for
DVSelections anywhere.

DOH! DOH! DOH! DOH!
Looked over the code a dozen times and missed this little detailed
completely.
Thanks. Greg

Jan 13 '06 #9
Bahhh
if you want to create a pure virtual interface with pure virtual
destructor do
..h file:

class IMyPureVirtualC lass
{
public:
virtual ~IMyPureVirtual Class() = 0;

virtual void SomeInterfaceFu nction() = 0;
//...
};

..cpp file:
IMyPureVirtualC lass::~IMyPureV irtualClass()
{
//empty destructor (declared as pure virtual)
}

now, when implementing the interface you should keep interface
functions virtual.
destructors in class declerations should ALWAYS be virtual or else they
might not get called, and it might cause mem leaks.

cheers
mark wrote:
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
"Greg" <gr****@pacbell .net> wrote:
mark wrote:
In article <44************ ****@news.west. earthlink.net>,
David Harmon <so****@netcom. com> wrote:

> On Fri, 13 Jan 2006 14:34:38 +1300 in comp.lang.c++, mark
> <tr*****@ihug.c o.nz> wrote,
> >I was under the impression if the virtual function is not defined in a
> >subclass, the parent class's function will be used.
>
> Yes, however if you declare it in the subclass declaration, that
> means you are promising to define it somewhere.
>

I didn't define the virtual in the subclass, only the base class (and
it's not pure).


If a function is declared virtual in a base class, it is also virtual
in every derived class whether or not the derived class uses the
"virtual" keyword in its declaration of the function.

I suspect that you have not defined the (virtual) destructor for
DVSelections anywhere.

DOH! DOH! DOH! DOH!
Looked over the code a dozen times and missed this little detailed
completely.
Thanks.
Greg


Jan 23 '06 #10

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

Similar topics

2
1972
by: Jimmy Johns | last post by:
Hi all, I have some class hierarchy as follows: class S {}; class A {}; class B {public: vector<S*> v_; virtual ~B();}; class C : public virtual A, public virtual B { // do I need to define virtual ~C() so that B can be properly destructed?}; in fact, I don't even know if a destructor in C is even needed if I don't do
19
2351
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
8
2848
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl list class must Ioverride in order for this to work?
2
1779
by: Bonj | last post by:
Hello Can anyone assist with the following class hierarcy problem? I have a series of window classes, the object model currently being as such: Window / | \ / | \ MDIClientWindow | TreeViewWindow WndProcWindow / \
4
385
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
14
12149
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
3
4552
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
4
2346
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which template is created and I tried to define a virtual method, that's called from the base template ctor. Here's my example code: #include <iostream>
12
2666
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read up on virtual inheritance and from my understanding this should work fine but I haven't found any docs that use such a tangled example. The gcc output containing the errrors: Example.cpp: In member function 'virtual IObject*...
0
9519
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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...
0
9042
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.