473,738 Members | 3,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual Constructors and Destructors

hii
Would somebody clear my doubts on following qustions .

What are virtual constructors and virtual destructors ? Why can't we
have virtual constructors ?
What are demerits of inheritence in C++ ?
waitng for the answers.

Apr 14 '06 #1
5 3777
Alok <ca**********@g mail.com> wrote:

What are virtual constructors and virtual destructors ? Why can't we
have virtual constructors ?


http://groups.google.com/group/comp....622bd42a13aa68

http://groups.google.com/group/comp....6104056841e03b

Please use Google *before* posting.

hth
--
jb

(reply address in rot13, unscramble first)
Apr 14 '06 #2
Alok wrote:
hii
Would somebody clear my doubts on following qustions .

What are virtual constructors and virtual destructors ? Why can't we
have virtual constructors ?
What are demerits of inheritence in C++ ?
waitng for the answers.


Your second question denies the existence of a virtual constructor which
happens to be the subject in your first question.

Ben
Apr 14 '06 #3

Alok wrote:
...[snip]...
waitng for the answers.


Rather demanding attidude. Instead of waiting for somebody to answer
your questions ( which appear to me that they might be homework ), why
not try to find the answer yourself!

-Brian

Apr 14 '06 #4
Alok wrote:
hii
Would somebody clear my doubts on following qustions .

What are virtual constructors and virtual destructors ?
The former is something non-existant that people keep asking about. The
second is needed when you want to delete an object of a derived class
through a pointer to a base class.
Why can't we have virtual constructors ?
Because they don't make sense. This topic is raised here at least once per
month, so just search for the last thread about this. Google Groups is your
friend.
What are demerits of inheritence in C++ ?


Not sure what you mean by that.

Apr 14 '06 #5
Virtual constructor:

1. From basic c++ concepts, we know constructors never derive into the
derived classes.
2. virtual functions used only when you need to have different
behaviour of the function in
derived class.
3. You cannot override the constructors and it is used to initialize
the objects.

There is no concept of calling base class constructor which is
overriden in derived class.

class x {
virtual x() {}
}

class y: public x
{
x() {} // you cannot override x defined in x.
}

x *p = new y;
p->x(); it never invokes derived class x defination.
You can write you own class and class member function that behaves like
virtual constructor.

class x {
}

class y: public x {
}

class z: public x {
}
class T {
x*p;

public:
T( int otype ) {
if ( otype == y)
p = new y;
else
p = new z;
}

x* readobject () {
return p;
}

}

T *p = new T( y type );
x* p1 = p-> readobject();


Rolf Magnus wrote:
Alok wrote:
hii
Would somebody clear my doubts on following qustions .

What are virtual constructors and virtual destructors ?


The former is something non-existant that people keep asking about. The
second is needed when you want to delete an object of a derived class
through a pointer to a base class.
Why can't we have virtual constructors ?


Because they don't make sense. This topic is raised here at least once per
month, so just search for the last thread about this. Google Groups is your
friend.
What are demerits of inheritence in C++ ?


Not sure what you mean by that.


Apr 16 '06 #6

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

Similar topics

1
1777
by: Shubhadeep | last post by:
Dear Sir, Can anybody please tell me why cannot we have virtual destructors like virtual constructors in C++ ? Thanx, Regards, Shubhadeep
7
1888
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the destructors. What I am expecting is, if the destructor is declared as virtual in base, the destructors of all its derived classes also should be virtual always. What exactly is the reason for not having it so?
26
3977
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol in file vtable for vechile /var/tmp//ccC9yD6Z.o
4
1264
by: Zeng | last post by:
I often run into situation where I would like to have a method such as that has derived behavior similar to destructors in c++, is that possible? public BaseClass { private int m_dataInBase; public BaseClass() {m_dataInBase = 0} public void SetDataBase( int val ) { m_data = val; } virtual public void ClearData()
7
2563
by: vaividhya | last post by:
We can have virtual destructors.Why we can't have virtual constructors?
5
11360
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
7
2112
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
17
3543
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
8
2334
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
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
9334
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
9208
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
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...
1
6750
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.