This question already has an answer here:
Why can in-class initializers only use = or {}? [duplicate]
(1 answer)
Closed 5 years ago.
This is the example from Effective modern c++.
class Widget {
…
private:
int x{ 0 }; // fine, x's default value is 0
int y = 0; // also fine
int z(0); // error!
};
direct initialization using ()
Inside the class treats the below
int z(0);
As a function as and expects parameter .As result error
Expected parameter declarator
alternatively can be done
class Widget {
private:
int x{ 0 };
int y = 0;
int z;
public:
Widget():z(0){}
};
Related
This question already has an answer here:
Unable to initialize private const member [duplicate]
(1 answer)
Closed 4 years ago.
Why can't I do this in C++?
struct SomeStruct
{
public:
SomeStruct(const int someInt)
{
m_someInt = someInt;
}
private:
const int m_someInt;
};
Should the private field just be a regular integer?
You're assigning someInt to m_someInt, which is illegal. But initialization is okay.
struct SomeStruct
{
public:
SomeStruct(const int someInt) : m_someInt(someInt)
{
}
private:
const int m_someInt;
};
More info: Constructors and member initializer lists
Value cannot be assigned to a const storage. It can be only initialized. In case of class member variable it would be done in initialization list.
struct SomeStruct
{
public:
SomeStruct(const int someInt) : m_someInt(someInt)
{
}
private:
const int m_someInt;
};
Sometimes in-class initialization is enough:
template <int Val>
struct SomeStruct
{
public:
private:
const int m_someInt = Val;
};
Usually confusion stems from the fact that programmer doesn't see difference between two cases:
// 1) Declaring storage of int object that initially contains value 5
int a = 5; // It's a declaration.
// 2) Declaring storage of int object that contains undetermined value
int b; // Declaration
b = 5; // Assigning a value to it. It's a statement.
In your case m_someInt = someInt; is a statement that expects lvalue before =, and m_someInt is not a legal lvalue because it is const.
This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 4 years ago.
class Sales_data {
public:
Sales_data(int i, int j, int k) : x(i), y(j), z(k) {
}
private:
int x,y,z;
};
In the above code(more specifically in Sales_data constructor(recited below)), I don't understand the use of colon and comma separated list.
Sales_data(int i, int j, int k) : x(i), y(j), z(k) {
}
I have never seen a colon(":") following any function/constructor parameter list. What does this colon mean/signify here ?
Moreover, what is this comma separated list after colon ?
You may be confused, because the member variable name (x) is same as the function parameter (also x), which you can always avoid for clarity. Simplified code can look like so.
add_x(int x1) : x(x1) // a contructor that initializes the member vaiable x to x1
{
}
Still confused? then you can go for this ( not so optimize though)
add_x(int x1)
{
x = x1;
}
This is a constructor
This is not a standard function/method. Each class (struct) can have constructor(s). The constructor has the same name as the class and can optionally take parameters.
struct add_x {
int x;
add_x(int x) : x(x) {} // This is a constructor with one paramter
};
To make it easier to read let us format it better.
struct add_x {
int x;
add_x(int x) // Constructor that takes one argument.
: x(x) // This is called the initializer list.
{} // This is the body of the constructor.
};
The initializer list allows you to list out the member variables (comma separated) and initialize them before the body of the constructor is executed.
In this case the member x is initialized with the parameter x.
#include <iostream>
int main()
{
add_x test(5);
std::cout << test.x << "\n"; // Should print 5
// Note in my example I have removed the private
// section so I can read the value x.
}
This question already has answers here:
assignment operator within function parameter C++
(3 answers)
Closed 5 years ago.
Here is a modified code from geeks for geeks.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() {return value;}
};
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
What does parameter in function Test(int v=0) means?
That is default value for v parameter, which is the v variable will use that value if there is no value passed
This question already has an answer here:
decltype in class method declaration: error when used before "referenced" member is declared
(1 answer)
Closed 8 years ago.
My compiler is GCC 4.9.0.
struct A {
int n;
auto f() -> decltype(n) { // OK
return n;
}
};
struct B {
auto f() -> decltype(n) { // error: 'n' was not declared in this scope
return n;
}
int n;
};
int main() {
return A().f() + B().f();
}
Why does the order of the class members matter?
The declarations are compiled in order. It's the same reason you can't write:
int y = x;
int x = 5;
The bodies of inline functions (incl. c-tor initiailizer lists) are processed later (parsed first of course, but the names aren't looked up until after the class definition is complete) so they can refer to class members that are on later lines.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the explicit keyword in C++ mean?
I do not understand the following. If I have:
class Stack{
explicit Stack(int size);
}
without the keyword explicit I would be allowed to do:
Stack s;
s = 40;
Why would I be allowed to do the above if explicit wasn't provided?? Is it because this is stack-allocation (no constructor) and C++ allows anything to be assigned to the variable unless explicit is used?
This line
s = 40;
is equivalent to
s.operator = (40);
Which tries to match the default operator = (const Stack &). If the Stack constructor is not explicit, then the following conversion is tried and succeeds:
s.operator = (Stack(40));
If the constructor is explicit then this conversion is not tried and the overload resolution fails.
hey its pretty simple .
the explicit key word only stops complier from automatic conversion of any data type to the user defined one.. it is usually used with constructor having single argument .
so in this case u are jus stopping the complier from explicit conversion
#include iostream
using namespace std;
class A
{
private:
int x;
public:
A(int a):x(a)
{}
}
int main()
{
A b=10; // this syntax can work and it will automatically add this 10 inside the
// constructor
return 0;
}
but here
class A
{
private:
int x;
public:
explicit A(int a):x(a)
{}
}
int main()
{
A b=10; // this syntax will not work here and a syntax error
return 0;
}