473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing member object's virtual functions

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;
};

class test1
{
protected:
void fun() {}

private:
test t1;
};

Apr 9 '07 #1
7 1763
On 2007-04-09 13:36, v4vijayakumar wrote:
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;
};

class test1
{
protected:
void fun() {}

private:
test t1;
};
Can't do that. test is purely virtual so you can't instantiate it in
test1. Are you sure you don't want to use inheritance? That's the usual
usage of virtual:

class test1 : public test
{
protected:
void foo() {/*...*/}
};

--
Erik Wikström
Apr 9 '07 #2
v4vijayakumar wrote:
Is it possible to implement member object's virtual functions, in the
containing class? If not, is it possible to simulate this behavior?
No, it's not possible. You can do what you want by creating an
inheritance hierarchy of classes. You can implement the virtual function
in derived class.

Fei
>
ex:

class test
{
protected:
virtual void fun() = 0;
};

class test1
{
protected:
void fun() {}

private:
test t1;
};
Apr 9 '07 #3
v4vijayakumar wrote:
Is it possible to implement member object's virtual functions, in the
containing class?
No. You cannot define member functions for single objects, only for whole
classes.
If not, is it possible to simulate this behavior?
You mean, other than deriving from "test", implementing it there, then make
an instance of that derived class a member of "test1"? No.
Your class "test" is abstract and can not be instantiated.
ex:

class test
{
protected:
virtual void fun() = 0;
};

class test1
{
protected:
void fun() {}

private:
test t1;
};
Apr 9 '07 #4
On Apr 9, 4:36 pm, "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co m>
wrote:
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;

};

class test1
{
protected:
void fun() {}

private:
test t1;

};
implementation by containment (?)

Apr 10 '07 #5
On Apr 9, 7:36 am, "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co m>
wrote:
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;

};

class test1
{
protected:
void fun() {}

private:
test t1;

};
No, if the language allowed that then coupling would become a
nightmare.
Isn't class test meant to be abstract anyways?
Templates are not required but consider the implications:

#include <iostream>

class abstract
{
protected:
virtual void fun() = 0;
};

class concrete : public abstract
{
public:
void fun() { std::cout << "concrete::fun( )\n"; }
};

class another : public concrete
{
public:
void fun() { std::cout << "another::fun() \n"; }
};

// type M must implement void fun()
template< typename M >
class test
{
M m;
public:
void fun() { m.fun(); }
};

int main()
{
test< concrete t;
t.fun();

test< another a;
a.fun();
}
/*
concrete::fun()
another::fun()
*/

Apr 10 '07 #6
On Apr 10, 9:23 am, "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co m>
wrote:
On Apr 9, 4:36 pm, "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co m>
wrote:
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;

};

class test1
{
protected:
void fun() {}

private:
test t1;

};
implementation by containment (?)
I don't know how usefull this, but I would like to know some valid
reasons to avoid this.

Salt_Peter wrote:
No, if the language allowed that then coupling would become a
nightmare.
Sorry, I don't see (couldn't understand ?!) any coupling issues.

Let me try to list down some more info. (May be, I am wrong)

1. while creating member object for an abstract class, the containing
class can be looked for valid implementation for the virtual
functions.

2. If there is no valid implementations then the member object
creation can be disallowed (results in a compile time error),
otherwise, member object construction can be allowed.

3. Any calls to member object's virtual function can be directed to
the implementation provided by the containing class.

4. I think, there is no need to maintain vtable for this containment
hierarchy.

5. old code calling new code ?!

Apr 10 '07 #7
On Apr 10, 10:48 am, "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co m>
wrote:
On Apr 10, 9:23 am, "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co m>
wrote:
On Apr 9, 4:36 pm, "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co m>
wrote:
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;
};
class test1
{
protected:
void fun() {}
private:
test t1;
};
implementation by containment (?)

I don't know how usefull this, but I would like to know some valid
reasons to avoid this.

Salt_Peter wrote:
No, if the language allowed that then coupling would become a
nightmare.

Sorry, I don't see (couldn't understand ?!) any coupling issues.
If the containing class held the implementation of a function declared
elsewhere than surely there would be coupling issues.
>
Let me try to list down some more info. (May be, I am wrong)

1. while creating member object for an abstract class, the containing
class can be looked for valid implementation for the virtual
functions.
There is no 'objects' in a class. A class/struct is a blueprint.
An instance of a class is an object,
and an abstract class can never be instantiated to be an object or a
member of an object.
Thats what 'abstract' means: the class is not allowed to be used to
instantiate a concrete object.
The term 'abstract' means you must derive and implement whatever is
pure-virtual before instantiating a derived object. It implies an
inheritence tree.

You should consider reading about abstract types, they are a powerful
concept (considerably more powerfull than you think them to be). Good
examples of these is std::ostream where the abstract type can be used
as a reference to a derived object safely.
>
2. If there is no valid implementations then the member object
creation can be disallowed (results in a compile time error),
otherwise, member object construction can be allowed.
Thats exactly what the example with the templated class does.
>
3. Any calls to member object's virtual function can be directed to
the implementation provided by the containing class.
no, since the containing class is not in the inheritance hierarchy.
Think of it this way: what you are trying to do is in fact doable. You
could conceivably use a function pointer and the appropriate
arguements/references to redirect a member function to call a sequence
of events held in the container object. However thats bad design, bug-
ridden and very expensive to maintain. Its ugly, hard to read, hard to
debug, difficult to explain, a bad concept, etc...
>
4. I think, there is no need to maintain vtable for this containment
hierarchy.
casting usually is more expensive than polymorphic mechanisms.
And most of the time, casting is much, much slower as well as
inherently unsafe.
Keep it simple and structured or pay the price.
>
5. old code calling new code ?!
what?
Apr 10 '07 #8

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

Similar topics

5
9362
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
11
4581
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member...
12
2065
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager, so he will send us all on a conversion course. One of many reasons is that our code is littered with examples of: SUBROUTINE PRINT_ITEM(ITEM,...
3
1974
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the...
22
2921
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give me some hints on what might be wrong? // BEGIN OF SAMPLE CODE class Strategy
5
651
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
13
2509
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
10
2077
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think the reason behind this restriction... i just want to want a base class to be abstract so as to avoid object slicing into the base type from derived...
5
4642
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
7734
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. ...
0
7979
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...
1
7497
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
7826
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
6065
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...
0
3512
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...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
781
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.