How do I invoke non-default constructor for a member variable?
If you are using C++11 or later, you can write
class X{
MyClass<10> mcTen = {1, 5};
}
Demo 1.
Prior to C++11 you would need to do it in a constructor's initializer list:
class X{
MyClass<10> mcTen;
X() : mcTen(1, 5) {
}
}
Demo 2.
c++ calling non-default constructor as member
Use initialization list.
class B {
public:
B(int i) {}
};
class A {
B m_B;
public:
A() : m_B(17) {}
};
BTW, to reset m_B
somewhere outside of the constructor, the correct syntax is:
m_B = B(17);
How do you use the non-default constructor for a member?
You need to call a(int) explicitly in the constructor initializer list:
b() : aInstance(3) {}
Where 3 is the initial value you'd like to use. Though it could be any int. See comments for important notes on order and other caveats.
Initializing a member class of an object using a non-default constructor in C++
What you want is an initializer list. For example:
Tree::Tree(int fruit_num)
: apples(fruit_num) // Initializes "apple" with "fruit_num"
{
}
You simply add a colon (:
) after the constructor parameters and before the opening brace {
. You can separate different member constructors with commas (,
). Example:
Tree::Tree(int fruit1, int fruit2) : apples(fruit1), bananas(fruit2) {
}
Using default and non default constructors in my application
When you use
BOOK1 = new BOOKItem();
You are calling the default constructor (Construction without having any arguments)
After taking user input you would call the Non Default Constructor
bookID = keyboard.nextInt();
numberInStock = keyboard.nextInt();
code = keyboard.nextInt();
price = keyboard.nextDouble();
BOOK2 = new BOOKItem(bookID, numberInStock, price, code);
Use the above code to use the Parameterized Constructor (Non Default constructor)
This is called constructor overloading.
So for the objects you want to assign default values you call the default constructor
For the object you have information available with variables you sent them in the constructor that you have defined with parameters, To assign that value
Using default constructor in non-default constructor
Yes it is possible with the help of delegating constructor.
This feature, called Constructor Delegation, was introduced in C++ 11. Take a look at this,
#include<iostream>
using namespace std;
class Test{
private:
int age;
int createdAt;
public:
//Here is the defualt constructor.
Test(){
createdAt = 0;
};
//Non-default constructor calling default constructor.
Test(int age): Test(){ // delegating constructor
this->age = age;
};
int getAge(){
return age;
}
int getCreatedAt(){
return createdAt;
}
};
int main(int argc, char *argv[]) {
Test t(28);
cout << t.getCreatedAt() << "\n";
cout << t.getAge() << "\n";
return 0;
}
Call non-default constructor as member initialization
Default member initializer (since C++11) only supports brace or equals initializer; you could use brace initializer here.
class A
{
B b{parameters...};
Other thing = 3;
};
Related Topics
Boost Asio Ssl Async_Shutdown Always Finishes with an Error
Technically, How Do Variadic Functions Work? How Does Printf Work
Gcc Equivalent of Ms's /Bigobj
Why Lifetime of Temporary Doesn't Extend Till Lifetime of Enclosing Object
Problem with Std::Map::Iterator After Calling Erase()
Do Current X86 Architectures Support Non-Temporal Loads (From "Normal" Memory)
Use of the & Operator in C++ Function Signatures
Clearing Terminal in Linux with C++ Code
Cmake Unable to Determine Linker Language with C++
How to Write C++ Getters and Setters
Cancelling Boost Asio Deadline Timer Safely
How to Test Whether a C++ Class Has a Default Constructor (Other Than Compiler-Provided Type Traits)
How to Give Priority to Privileged Thread in Mutex Locking