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

Virtual Constructor

Virtuelle Konstruktoren

Ich habe gerade gelesen:
Konstruktoren dürfen nicht virtuell deklariert werden,
da beim Erzeugen von Objekten immer die tatsächliche Klasse
bereits zur Compilezeit festgelegt wird.

Leider verstehe ich diesen Satz nicht und wende mich deshalb an
die Newsgroup!

Kann mir bitte jemand erklären, was die obige Erklärung besagt?
<< die tatsächliche Klasse liegt zur Compilezeit fest bei der
Erzeugung
von Objekten >>

Danke für Eure Hilfe.

Nov 1 '06 #1
3 5346
* Zeh Mau:
>
Danke für Eure Hilfe.
Please post in English, not Krakatoan.
--
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?
Nov 1 '06 #2
Zeh Mau wrote:
Virtuelle Konstruktoren

Ich habe gerade gelesen:
Konstruktoren dürfen nicht virtuell deklariert werden,
da beim Erzeugen von Objekten immer die tatsächliche Klasse
bereits zur Compilezeit festgelegt wird.

Leider verstehe ich diesen Satz nicht und wende mich deshalb an
die Newsgroup!
Ist Dir aufgefallen, dass dies eine englischsprachige Gruppe ist?

class Base { public: virtual void foo(); };
class Derived { public: void foo(); };

Base* base = new Derived();
base->foo();

The last line calls Derived::foo because base points to a an object of
type Derived.
So the actual object behind the pointer decides which member functions
gets called.

When you call a constructor you don't have any object that can decide
which class' constructor gets called. How should polymorphic
constructors work?
Kann mir bitte jemand erklären, was die obige Erklärung besagt?
<< die tatsächliche Klasse liegt zur Compilezeit fest bei der
Erzeugung
von Objekten >>
Base* base = new Derived();
The compiler knows that it has to generate code that calls
Derived::Derived(). So it's known at compile time which class'
constructor shall be called. There is no need to defer this dicision to
run time.
Danke für Eure Hilfe.
You're welcome.

Nov 1 '06 #3
Wenn du auf deutsch Fragen stellen willst, wäre de.comp.lang.iso-c++
geeigneter.
Zeh Mau wrote:
Virtuelle Konstruktoren

Ich habe gerade gelesen:
Konstruktoren dürfen nicht virtuell deklariert werden,
da beim Erzeugen von Objekten immer die tatsächliche Klasse
bereits zur Compilezeit festgelegt wird.

Leider verstehe ich diesen Satz nicht und wende mich deshalb an
die Newsgroup!

Kann mir bitte jemand erklären, was die obige Erklärung besagt?
<< die tatsächliche Klasse liegt zur Compilezeit fest bei der
Erzeugung
von Objekten >>
Nun, wenn du ein Objekt erzeugst, gibst du dabei immer die konkrete Klasse
an. Wie soll das auch anders gehen? Virtuelle Memberfunktionen suchen zur
Laufzeit die Klasse des Objekts raus, für das sie aufgerufen werden, damit
die richtige Implementation ausgeführt wird. Wenn du das Objekt erzeugen
willst, ist aber logischerweise noch keins da. Daher kann der Compiler auch
die Klasse nicht automatisch erkennen. Du kannst du dem Compiler
schließlich nicht sagen: "Erzeuge mal ein Objekt von der Klasse, der dieses
Objekt dann nachher angehören wird". Du mußt ihm explizit die Klasse sagen,
und daher ergeben virtuelle Konstruktoren gar keinen Sinn.

Nov 1 '06 #4

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

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
6
by: Thomas Matthews | last post by:
Hi, Is placing the keyword "virtual" in front of a constructor allowed as in the sample below? class TTable { virtual TTable(); }; My compiler, Borland Builder 5.2, has system libraries
3
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
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...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
4
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which...
3
by: Klaas Vantournhout | last post by:
Hi, Recently I obtained a problem with virtual inheritance when implementing it in multiple files. To present the problem I have included at the bottom of this post the code of the 4 files. I...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
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;"...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...

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.