default-constructor

Why in C# 3.0, when we overload constructor of a specified class, we should write default constructor in class body?

Hi Why in C# 3.0, when we overload constructor of a specified class, we should write default constructor in class body? As far as I know, It was no need to do so. class Test { public int ID {get; private set;} public int Name {get; private set;} public Test() { } public Test(int id, int name) ...

Calling base class constructor

In the program below, is the line Derived(double y): Base(), y_(y) correct/allowed? That is, does it follow ANSI rules? #include <iostream> class Base { public: Base(): x_(0) { std::cout << "Base default constructor called" << std::endl; } Base(int x): x_(x) { std::cout << "Base constructor called with x = " << x...

How can I make the compiler create the default constructors in C++?

Is there a way to make the compiler create the default constructors even if I provide an explicit constructor of my own? Sometimes I find them very useful, and find it a waste of time to write e.g. the copy constructor, especially for large classes. ...

Purpose of Explicit Default Constructors

I recently noticed a class in C++0x that calls for an explicit default constructor. However, I'm failing to come up with a scenario in which a default constructor can be called implicitly. It seems like a rather pointless specifier. I thought maybe it would disallow Class c; in favor of Class c = Class(); but that does not appear to b...

variadic constructors

Are variadic constructors supposed to hide the implicitly generated ones, i.e. the default constructor and the copy constructor? struct Foo { template<typename... Args> Foo(Args&&... x) { std::cout << "inside the variadic constructor\n"; } }; int main() { Foo a; Foo b(a); } Somehow I was expecting this to ...

Is calling base class constructor always necessary in C++?

Suppose I have some class C, and I inherit from it and name this class D. Do I always have to call C's default constructor as in this example: class C { public: C() { ... } }; class D { public: D() : C() { ... } }; Note that C has only the default constructor. Do I have to call it from D? I couldn't figure out...

C++: Creating an uninitialized placeholder variable rather than a default object.

I'm moving from Java to C++ right now and I'm having some difficulties whenever a commonly used concept in Java doesn't map directly into C++. For instance, in Java I would do something like: Fruit GetFruit(String fruitName) { Fruit fruit; if(fruitName == "apple") fruit = new Fruit("apple"); else if(fruitName == "banana") f...

Correct use of this. in a class constructor

I was browsing some documentation for a physics library for XNA and noticed an example someone had used for creating a class for a Car. This is a pretty simple example: Class Car { private float gravity; private float maxSpeed; public Car(float gravity, float maxSpeed) { this.gravity = gravity; this.maxSpee...

Declare an object in C++ w/o creating it?

Is this possible? For example if i write Car myCar; Then the constructor taking no arguments of Car is called. It results in an error if there is only a constructor taking arguments. In Java I can easily declare an object and create it later using the exact same statement as above. ...

Should we always include a default constructor in the class?

I have been asked this question by a colleague that should we always include a default constructor in a class? If so, why? If no, why not? Example public class Foo { Foo() { } Foo(int x, int y) { ... } } I am also interested to get some lights on this from experts. ...

Class inherited from class without default constructor

Right now I have a class A that inherits from class B, and B does not have a default constructor. I am trying the create a constructor for A that has the exact same parameters for B's constructor, but I get: error: no matching function for call to ‘B::B()’ note: candidates are: B::B(int) How would I fix this error? ...

C++ unrestricted union workaround

#include <stdio.h> struct B { int x,y; }; struct A : public B { // This whines about "copy assignment operator not allowed in union" //A& operator =(const A& a) { printf("A=A should do the exact same thing as A=B\n"); } A& operator =(const B& b) { printf("A = B\n"); } }; union U { A a; B b; }; int main(int argc, c...

What is the code : base()

What is the purpose of base() in the following code? class mytextbox : TextBox { public mytextbox() : base() { this.Text = "stack"; } } ...