referencing a variable on multiple class objects [duplicate] - c++

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 };

Related

How to static_assert a null pointer inside a constructor [duplicate]

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.

C++ - Is there a way to create a custom "std::initializer_list" class that is defined using brackets "[]" instead of curly brackets? [duplicate]

This question already has answers here:
How come std::initializer_list is allowed to not specify size AND be stack allocated at the same time?
(4 answers)
Closed 8 months ago.
Is there a way to create a custom "std::initializer_list" class that is defined using brackets "[]" instead of curly brackets?
Code example.
class BracketsInitializerList {
// class definition here
// ...
};
class SomeClass {
public:
SomeClass(BracketsInitializerList<int> x) { ... }
};
int main() {
SomeClass x = [0, 1, 2, 3, 4, 5];
}
No. You can't even create a custom replacement for std::initializer_list (that uses {...}), since it uses compiler magic.
But you can create an imitation: create a global variable with an overloaded operator[] (which can accept multiple parameters in C++23), and do my_var[1, 2, 3].

How to initialize a large private array in a class using constructor(just a constructor) in c++? [duplicate]

This question already has answers here:
Zero-Initialize array member in initialization list
(3 answers)
C++ Initializing Non-Static Member Array
(9 answers)
How to initialize all members of an array to the same value?
(26 answers)
Closed 3 years ago.
Suppose we have a class like the following one:
class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;};
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){}
As you can see we can initialize our private members' aa, bb, cc using the myprogram() constructor.
Now, suppose I have a large private array G_[2000]. how I could initialize all the values of this array equal to 0 using a constructor.
class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;
double G_[2000];};
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){}
Use std::memset function in constructor's body.
For example,
myprogram::myprogram()
: aa{0.0}, bb{0.0}, cc{0.0}
{
std::memset(G_, 0, 2000 * sizeof(double));
}
However, if you use braces {} in your initializer list, it will set default-initialize object (In case of array, it will fill it by zeroes).
You can write:
myprogram::myprogram()
{
for(int i=0;i<2000;i++)
G_[i]=0;
}

Initializing all doubles in a class to zero [duplicate]

This question already has answers here:
Is there a way to make a C++ struct value-initialize all POD member variables?
(3 answers)
Closed 1 year ago.
I have a struct that only has double member variables, e.g.
struct foo { double a, b, c; };
Another class has a std::vector<foo> v member that calls the std::vector<foo>::vector(size_t) constructor in the initializer list.
What I'd like to do is write a default constructor for foo so that all the doubles in it are initialized to zero, without having to write
foo(): a(0), b(0), c(0) { }
I keep needing to add more variables to foo, but it doesn't make sense to have them be elements of a container like std::array<double>, because they all serve distinct purposes.
Since you've tagged this as C++14, you can initialize member variables like this without having to initialize them in a constructor:
struct foo {
double a = 0.0;
double b = 0.0;
double c = 0.0;
};

what compiler does internally for initializing a variable and assigning a variable while constructing object? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why should I prefer to use member initialization list?
Class A has a member variable i. i can be initialized or assigned during object creation.
A) Initialise
class A {
int i;
public:
A(int _i) : i(_i){}
}
B) assign
class A {
int i;
public:
A(int _i) : { i = _i}
}
My question is what is the basic difference between these 2 approach?
The difference lies in which C++ mechanism is used to initialize i in your class. Case (A) initializes it via constructor, and case (B) uses the assignment operator (or a copy constructor if no assignment operator is defined).
Most C++ compilers would generate exactly the same code for this particular example, because you're using int, which is a "plain old data" type. If i were a class type, it could make a great deal of difference.