How to static_assert a null pointer inside a constructor [duplicate] - c++

This question already has answers here:
static_assert evaluates non constant expression
(2 answers)
Why is comparing two parameters of a constexpr function not a constant condition for static assertion?
(2 answers)
Closed 3 months ago.
I am trying to crate a static_assert in my constructor . But if fails to work.
I saw the usage of static assert with templates, but how do I use them in a global statically allocated class?
class A
{
private:
int *a_;
void UseMyA() { std::cout << "" << *a_ << end; }; // protect all functions that use a_ without added code.
public:
A(int *a) { a_ = a ; static_assert(a_);};
}
// global
int *a =NULL;
A global_A(a);
Could not find a proper example.

Related

referencing a variable on multiple class objects [duplicate]

This question already has answers here:
What is the difference between "::" "." and "->" in c++ [duplicate]
(8 answers)
(->) arrow operator and (.) dot operator , class pointer
(6 answers)
Closed 8 months ago.
Say I have a class with a public variable (bad practice, I know), and in the main function, I want to create 3 class objects, how could I assign different values to that class variable?, something like:
class C{
public:
int foo;
};
int main(){
C co[3];
co[0]->foo = 20;
co[1]->foo = 40;
co[2]->foo = 80;
}
Since you have different value for each object, you need to initialize them one by one.
Fortunately there is a simple way to kind of do it in s single swoop without using individual assignments: Initialize them at definition.
Create a suitable constructor:
class C
{
int foo;
public:
C(int foo)
: foo(foo)
{
}
};
Then you can initialize the array like any other array:
C co[3] = { 20, 40, 80 };

Can anybody explain a typical scenario where Const function is necessary? [duplicate]

This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 2 years ago.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
// We get compiler error if we add a line like "value = 100;"
// in this function.
int getValue() const {return value;}
};
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
Can anybody explain a typical practical scenario where Const function is necessary?
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided.
A const member function can be called by any type of object. Non-const functions can be called by non-const objects only.
https://www.tutorialspoint.com/const-member-functions-in-cplusplus

classname classobject(); does not invoke any constructor [duplicate]

This question already has answers here:
My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?
(5 answers)
Closed 3 years ago.
If I have a following situation:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "Inside A" << endl;
}
};
int main() {
A a();
return 0;
}
Why is the constructor not invoked?
If something looks like a function declaration, the C++ standard requires it be treated as a function declaration.
A a(); does not default-construct an object a of type A. It declares a function a that takes no input parameters and returns an A object as output.
To default-construct a variable a, you need to drop the parenthesis:
A a;
Or, in C++11 and later, you can use curly braces instead of parenthesis:
A a{};

Which constructor will be called? [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
What does X f() mean?
(4 answers)
Closed 4 years ago.
Why there is no output?
Why doesn’t an ambiguity error occur?
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Class A";
}
A(int a = 0){
cout << "A";
}
};
int main() {
A a();
return 0;
}
So when we create an object “a” of type “A” A a();, compiler calls a constructor without parameters, doesn’t he?
In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. A constructor is called depending upon the number and type of arguments passed.
While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.

c++ nested constructors call issue [duplicate]

This question already has answers here:
My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?
(5 answers)
Closed 5 years ago.
I have 2 test classes:
class B {
public:
B(int i) {
qDebug() << "B constructor ";
}
};
class A {
public:
A(B b) {
qDebug() << "A constructor ";
}
};
Now i want to create A object with B, thats what i do:
int i = 5;
A test (B(i)); //does not work
Code compiles without errors, but second line doesnt execute at all. I've made some tests, and code below works well:
int i = 5;
A test (B((int)i)); //works
A test (B(5)); //works
So, i guess compiler cant interpret 'i' as int inside B constructor call, but why?
This:
A test(B(i));
is the same as the:
A test(B i);
which is a function declaration, not a call to constructor due to a most vexing parse.
There is a rule that states (S. Meyers, "Effective Modern C++"):
anything that can be parsed as a declaration must be interpreted as
one
To avoid that use braced initialization (instead of parentheses ()) as functions can't be declared with {} braces:
A test{B(i)};
That being said there are no "nested constructors calls" in your example.