473,395 Members | 1,978 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,395 software developers and data experts.

virtual constructors?

hello,
i want to know that is virtual constructors possible? if not why?
also does virtual destructors functions are often used in C++
programming.
regards,
rahul.

Aug 23 '05 #1
4 1373
rahul8143 atgmail.com wrote:
i want to know that is virtual constructors possible? if not why?
Look at the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-20.8
also does virtual destructors functions are often used in C++
programming.


Yes, virtual destructors are often used in C++ programming.

Best regards,

Tom

Aug 23 '05 #2
ra*******@gmail.com wrote:
hello,
i want to know that is virtual constructors possible?
No.
if not why?
Because a virtual function call is dynamically dispatched to an object's
actual class. A constructor is called when an object gets created. At this
time, the class is well known, it wouldn't make sense to call the
constructor of another class instead, and there would be no way for the
runtime system to know which other class to dispatch it to.
also does virtual destructors functions are often used in C++
programming.


Yes.

Aug 23 '05 #3
ra*******@gmail.com wrote:
hello,
i want to know that is virtual constructors possible? if not why?
also does virtual destructors functions are often used in C++
programming.


A virtual function dispatches a function based on the actual type of
object being pointed at/referred to. Until the object exists (after the
ctor finishes execution) there IS no object whose type can be
determined.

If you ever destroy an object of a derived class via a pointer to a
base class, the destructor needs to be virtual. For example, this code
gives undefined behavior:

class base {};
class derived : public base {};

// ...
base *b = new derived;
// ...
delete b;

To get defined behavior, you need code more like this:

class base {
virtual ~base() {}
};

class derived : public base {};

// ...
base *b = new derived;
// ...
delete b;

In cases like this, the destructor is often defined only to make it
virtual, so it's fairly common for the destructor's body to be empty.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Aug 23 '05 #4
* Thomas Tutone:
rahul8143 atgmail.com wrote:
i want to know that is virtual constructors possible? if not why?

A constructor's job is to transform raw memory into a well-defined object.
Before that there is no object to dispatch a virtual call to a constructor,
and after that you cannot call a constructor again on this object (at least
not without using very low-level language features). That's the basic C++
constructor guarantee, that an object of type T receives one and only one T
constructor call, and that that happens before anything else.

Look at the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-20.8


See also
<url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 23 '05 #5

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

Similar topics

0
by: Alexander Stippler | last post by:
I've got an inheritance structure with two coupled "dreaded diamonds" like shown below: A / \ / \ B C \ / \ \ / \ D E \ /
3
by: Gianni Mariani | last post by:
In the code below, controller::controller() is never invoked, however, it appears there is no way to make a compile-time rule that this should not happen. The code below seems to make compilers...
26
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...
7
by: vaividhya | last post by:
We can have virtual destructors.Why we can't have virtual constructors?
5
by: Alok | last post by:
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...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
6
by: Carl R. Davies | last post by:
I was reading this link http://www.icce.rug.nl/documents/cplusplus/cplusplus14.html#l198 heading "14.10 Virtual Constructors" I am struggling to understand the issue the author is trying to...
17
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;"...
8
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
4
by: Rahul | last post by:
Hi Everyone, I understand that the constructors can't be virtual and parashift has the following example, to have an workaround for the constructors to be virtual, class Shape { public:...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
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...
0
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,...
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,...

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.