473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1746
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, "v4vijayakumar" <vijayakumar.subbu...@gmail.com>
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, "v4vijayakumar" <vijayakumar.subbu...@gmail.com>
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, "v4vijayakumar" <vijayakumar.subbu...@gmail.com>
wrote:
On Apr 9, 4:36 pm, "v4vijayakumar" <vijayakumar.subbu...@gmail.com>
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, "v4vijayakumar" <vijayakumar.subbu...@gmail.com>
wrote:
On Apr 10, 9:23 am, "v4vijayakumar" <vijayakumar.subbu...@gmail.com>
wrote:
On Apr 9, 4:36 pm, "v4vijayakumar" <vijayakumar.subbu...@gmail.com>
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
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,...
11
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...
12
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,...
3
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...
22
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...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
13
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...
10
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...
5
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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,...
0
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...

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.