C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Override and Virtual function checking in C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Leo Wong
Guest





PostPosted: Thu Aug 03, 2006 10:18 pm    Post subject: Override and Virtual function checking in C++ Reply with quote



Hi,

With a lot of classes and virtual functions to work with, it is
increasingly difficult to track incorrectly defined virtual functions
as described here in a previous thread:
http://groups.google.com/group/comp.std.c++/browse_thread/thread/57f9247880e
637d9/e862e15f8219a963?lnk=st&q=&rnum=5#e862e15f8219a963

I am trying to create some macros to address some but not all of the
problems:

#ifdef _DEBUG

#define VIRTUAL(Ret, Func) struct __dummy_##Func{}; virtual
__dummy_##Func* __virtual_##Func() { return 0; } virtual Ret Func
#define OVERRIDE(Ret, Func) void __override_##Func() {
__super::__virtual_##Func(); } virtual Ret Func
#define SEALED_OVERRIDE(Ret, Func) void __virtual_##Func(int) {
__super::__virtual_##Func(); } virtual Ret Func

#else

#define VIRTUAL(Ret, Func) virtual Ret Func
#define OVERRIDE(Ret, Func) virtual Ret Func
#define SEALED_OVERRIDE(Ret, Func) virtual Ret Func

#endif

class CBaseClass
{
public:

VIRTUAL(void,VFunc1)(int)
{
cout << "CBaseClass::VFunc1\n";
}

VIRTUAL(void,VFunc2)(double)
{
cout << "CBaseClass::VFunc2\n";
}
};

class CDerivedClass : public CBaseClass
{
public:

OVERRIDE(void,VFunc1)(int)
{
cout << "CDerivedClass::VFunc1\n";
}

SEALED_OVERRIDE(void,VFunc2)(double)
{
cout << "CDerivedClass::VFunc2\n";
}

// OVERRIDE(void,VFunc3)(char) // <-- compile time error, base
virtual
not defined
// {
// cout << "CDerivedClass::VFunc3\n";
// }
};

class CDerivedClass2 : public CDerivedClass
{
public:

OVERRIDE(void,VFunc1)(int)
{
cout << "CDerivedClass2::VFunc1\n";
}

// OVERRIDE(void,VFunc2)(double) // <-- compile time error, sealed
virtual function
// {
// cout << "CDerivedClass2::VFunc1\n";
// }
};


I haven't tested these extensively but one of the problem is that the
macros are not taking into account the parameters in the virtual
functions. The side effect is that they won't allow you to declare
multiple virtual functions with the same name in the same class with
different parameters.

Is there any facility in the boost library which will allow me to
'merge' all input parameter types into a string?

Thanks,

Leo


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thorsten Ottosen
Guest





PostPosted: Fri Aug 04, 2006 2:51 am    Post subject: Re: Override and Virtual function checking in C++ Reply with quote



Leo Wong wrote:
Quote:
Hi,

With a lot of classes and virtual functions to work with, it is
increasingly difficult to track incorrectly defined virtual functions

That is not the only problem *public* with virtual functions, but that's
another issue.

Quote:
I haven't tested these extensively but one of the problem is that the
macros are not taking into account the parameters in the virtual
functions. The side effect is that they won't allow you to declare
multiple virtual functions with the same name in the same class with
different parameters.

Somebody brought up this very simply idea, he is known in the C++
community (I forgot his name). The idea is simply and beautiful
(although it does not work at the interface level):

#define OVERRIDE( fun_and_params ) if( false ) fun_and_params

struct Base
{
virtual void foo() {}
virtual void foo1( int ) {}
};

struct Derived : Base
{
virtual void foo()
{
OVERRIDE( Base::foo() );
}

virtual void foo1( int i )
{
OVERRIDE( Base::foo1(i) );
}
};

int main()
{
Base* p = new Derived;
p->foo();
p->foo1(3);
}

-Thorsten

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gene Bushuyev
Guest





PostPosted: Fri Aug 04, 2006 2:52 am    Post subject: Re: Override and Virtual function checking in C++ Reply with quote



"Leo Wong" <wong.kam.keung (AT) gmail (DOT) com> wrote in message
news:1154623674.772326.196750 (AT) h48g2000cwc (DOT) googlegroups.com...
Quote:
Hi,

With a lot of classes and virtual functions to work with, it is
increasingly difficult to track incorrectly defined virtual functions

That should be a warning sign already. I found the percentage of virtual
functions even in the large projects is relatively small. Design teeming
with
virtual functions is highly suspicious. And it doesn't require exceptional
knowledge or skills to override virtual functions correctly. So it's hard
for me
to imaging this to be an "increasingly difficult" problem.

Quote:
as described here in a previous thread:

http://groups.google.com/group/comp.std.c++/browse_thread/thread/57f9247880e
637d9/e862e15f8219a963?lnk=st&q=&rnum=5#e862e15f8219a963

I am trying to create some macros to address some but not all of the
problems:

#ifdef _DEBUG

#define VIRTUAL(Ret, Func) struct __dummy_##Func{}; virtual
__dummy_##Func* __virtual_##Func() { return 0; } virtual Ret Func
#define OVERRIDE(Ret, Func) void __override_##Func() {
__super::__virtual_##Func(); } virtual Ret Func
#define SEALED_OVERRIDE(Ret, Func) void __virtual_##Func(int) {
__super::__virtual_##Func(); } virtual Ret Func


Bad idea. Macros are generally very bad idea and should be avoided unless
absolutely necessary. The second rule when creating macros, make their names

long and disgusting to prevent name clashes and discourage gratuitous use.
And
finally, the problem you mentioned is solved by learning good practices and
disciplined programming.

--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
To see what is in front of one's nose needs a constant struggle ~ George
Orwell


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
StepNRazor
Guest





PostPosted: Fri Aug 04, 2006 4:04 am    Post subject: Re: Override and Virtual function checking in C++ Reply with quote

I tried to reference the thread you suplied but the link did not work.
so from I seen in your thread I am going to reply to what I seen.

I belive using good desrciptive documentation in your class header
files would be more effective to users of your code to know the status
of your class members and how they relate to base or derived classes.

In your .cpp fole is where I would use ifdef debug switches and once
again documentation comments.

Your mechinism might work but in filtering classes by debug and or
inheritence issue but it lacks in describing ussage and describing the
ussage in documentation avialates the need for macro filtering.


Joe


Leo Wong wrote:
Quote:
Hi,

With a lot of classes and virtual functions to work with, it is
increasingly difficult to track incorrectly defined virtual functions
as described here in a previous thread:

http://groups.google.com/group/comp.std.c++/browse_thread/thread/57f9247880e
637d9/e862e15f8219a963?lnk=st&q=&rnum=5#e862e15f8219a963

I am trying to create some macros to address some but not all of the
problems:

#ifdef _DEBUG

#define VIRTUAL(Ret, Func) struct __dummy_##Func{}; virtual
__dummy_##Func* __virtual_##Func() { return 0; } virtual Ret Func
#define OVERRIDE(Ret, Func) void __override_##Func() {
__super::__virtual_##Func(); } virtual Ret Func
#define SEALED_OVERRIDE(Ret, Func) void __virtual_##Func(int) {
__super::__virtual_##Func(); } virtual Ret Func

#else

#define VIRTUAL(Ret, Func) virtual Ret Func
#define OVERRIDE(Ret, Func) virtual Ret Func
#define SEALED_OVERRIDE(Ret, Func) virtual Ret Func

#endif

class CBaseClass
{
public:

VIRTUAL(void,VFunc1)(int)
{
cout << "CBaseClass::VFunc1\n";
}

VIRTUAL(void,VFunc2)(double)
{
cout << "CBaseClass::VFunc2\n";
}
};

class CDerivedClass : public CBaseClass
{
public:

OVERRIDE(void,VFunc1)(int)
{
cout << "CDerivedClass::VFunc1\n";
}

SEALED_OVERRIDE(void,VFunc2)(double)
{
cout << "CDerivedClass::VFunc2\n";
}

// OVERRIDE(void,VFunc3)(char) // <-- compile time error, base
virtual
not defined
// {
// cout << "CDerivedClass::VFunc3\n";
// }
};

class CDerivedClass2 : public CDerivedClass
{
public:

OVERRIDE(void,VFunc1)(int)
{
cout << "CDerivedClass2::VFunc1\n";
}

// OVERRIDE(void,VFunc2)(double) // <-- compile time error, sealed
virtual function
// {
// cout << "CDerivedClass2::VFunc1\n";
// }
};


I haven't tested these extensively but one of the problem is that the
macros are not taking into account the parameters in the virtual
functions. The side effect is that they won't allow you to declare
multiple virtual functions with the same name in the same class with
different parameters.

Is there any facility in the boost library which will allow me to
'merge' all input parameter types into a string?

Thanks,

Leo

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.