C++ and C non-member multiple structs initialization - c++

How to initialize two or more structures with the same data? This has to be done at compile time to be as default data for const structures that are non-member global variables.
EDIT:
And what about C?

Works for me:
// header
struct Foo {
int a;
int b;
};
extern Foo const x;
extern Foo const y;
// cpp file
Foo const x{2, 3};
Foo const y = x;
Edit: reinterpreted the question a little.

One way is:
#define DATA { bla,bla,bla,bla }
var a = DATA;
var b = DATA;

You can use the copy constructor to initialize the structs. (This requires the use of a helper function to ensure correct order of initialization.) If the structures are POD, this should be as efficient as regular compile-time initialization:
struct A {
int a;
double c;
};
A initial_data() {
return { 1, 2.0 };
}
A a = initial_data();
A b = initial_data();

Related

can't create 2 arrays in class else incomplete type is not allowerd c++ [duplicate]

Consider the following code
struct dummy1 {
int b;
dummy1();
};
struct dummy {
dummy1 a[];
int c;
dummy();
};
int main()
{
dummy foo;
foo.a[0].b = 5;
foo.a[1].b = 6;
return 0;
}
dummy::dummy()
:a()
,c()
{
}
dummy1::dummy1()
: b()
{
}
I am getting an error "Incomplete type is not allowed" , As far as i know we get this error if the structure we are defining the object of is not defined or the header file is not included which is not the case strangely when I define my int c before a[] the code works so replacing this
struct dummy {
int c;
dummy1 a[];
dummy();
};
the code works just fine, Any ideas why this might be happening ?
P.S I am using VS2015
dummy1 a[]; is not valid. You must define the size of your array. If you don't know what size array you need in advance, use std::vector instead of a raw array:
struct dummy {
std::vector<dummy1> a;
int c;
};
int main()
{
dummy foo;
foo.a.resize(2);
foo.a[0].b = 5;
foo.a[1].b = 6;
}
The reason it compiles when you swap the order of your members is a feature of C called "flexible array members" that some compilers provide as an extension to C++. This allows you to declare an array-of-unknown-bound as the last member of a struct. No memory is reserved for that array when you declare an automatic or static variable of that type though. It's intended to be used with malloc:
struct dummy {
int c;
dummy1 a[];
};
int main() {
// allocate storage for 5 dummy1 objects in our flexible array
dummy* foo = malloc(sizeof(dummy) + 5 * sizeof(dummy1));
foo->a[0].b = 5;
foo->a[1].b = 6;
free(foo);
return 0;
}
Note that this is not a standard part of C++. It is a non-standard compiler extension, and does not play well with many C++ features. Compilers likely provide this extension mostly for compatibility with libraries written in C.
I guess it is the array a with size 0 in dummy. The standard doesn’t allow this.
Try:
struct dummy {
dummy1* a;
int c;
dummy();
};
Or:
struct dummy {
dummy1 a[1];
int c;
dummy();
};

Array initialization for class scope in c++11

I come from Java and Ruby so I have a hard time coding simple stuff in c++, c++ being harder to tame...
I want to initialize an array in the class constructor with predefined values that can be accessed by all methods in the class. Yeah, basic stuff.
In other words, I want to do something like this:
Box.h
class SomeClass
{
public:
SomeClass(int something);
SomeMethod();
DoSomething(int thing);
protected:
int _something;
int[] arr;
};
Box.cpp
SomeClass::SomeClass(int something) : something(_something)
{
arr ={16,2,45,65,45};
for (int x = 0; x < 5; x++)
arr[i] = arr[i] * _something;
}
SomeClass::SomeMethod(){
for (int x = 0; x < 5; x++)
DoSomething(arr[i]);
}
SomeClass::DoSomething(int thing){
//whatever
}
How?
#include <iostream>
//#include <initializer_list>
#include <array>
#include <algorithm>
class foo
{
public:
foo() : values({{16,2,45,65,45}})
{
// I left the below commented out in case you want to research more and use other kinds of types for doing it.
//std::initializer_list<int> list = {16,2,45,65,45};
//std::copy(std::begin(list), std::end(list), std::begin(values));
}
void print()
{
std::for_each(std::begin(values), std::end(values),
[](int v) { std::cout << v << ' '; });
}
private:
static const int SIZE = 5;
std::array<int, 5> values;
};
int main()
{
foo fooInstance;
fooInstance.print();
return 0;
}
The above is the best that I could do in a short period of time. I tested with the following compiler. You could use that as a starting point and see if you can learn other ways of doing it. Algorithms such as generate can also be used with lamda expressions in order to generate a controlled set of values (as opposed to a hard coded list as in our examples).
http://www.compileonline.com/compile_cpp11_online.php
You have options for initializing array data members, and two types or array you can use:
Using a C-style array,
struct Foo
{
int a[5] = {1,2,3,4,5}; // initialization at point of declaration
};
struct Bar
{
int a[5];
Bar() : a{1,2,3,4,5} {} // initialization in constructor initialization list
};
Or using an std::array:
struct Foo
{
std::array<int,5> a = {{1,2,3,4,5}}; // initialization at point of declaration
};
struct Bar
{
std::array<int,5> a;
Bar() : a{{1,2,3,4,5}} {} // initialization in constructor initialization list
};
If you can use C++11, there is support for uniform initialization which allows you to assign brace-enclosed lists of values in more contexts than the special case in C.

Is there a way in C++ to preinitialize structure or class without a constructors?

I mean: I've a bunch of various structures/classes and all this a splendor shall be initialized with known in advance values. Those structures/classes will never be used other way except the preinitialized one, so there is no any need for constructor -- it's just a waste of extra memory, extra CPU cycles in program, and extra space in source code.
If you have access to a C++11 compiler, you can mark your constructors as constexpr to have them run at compile time. The benefit of this is that way down the road you can still construct your objects at runtime. e.g.
struct Point2D {
constexpr Point2D(int x, int y) : x_{x}, y_{y} {}
int x_, y_;
};
And now you can use Point2D's constructor to initialize it at compile time, instead of runtime:
Point2D p{3, 4}; // no runtime overhead.
Structures and classes can be initialized, in limited circumstances.
struct splendor {
int i, j;
char *name;
};
splendor iforus = { 1, 2, "Extra!" };
Additionally, if you never need the name of the type of the structure:
struct {
int k;
float q;
} anon_e_mouse = { 1, 2.3 };
You can just initialize the members at the point of declaration:
struct Foo
{
int i = 42;
double x = 3.1416;
std::string name = "John Doe";
};
This will set up the default values for all instances:
Foo f;
std::cout << f.i << std::endl; // prints 42
Note that this does not work with C++03, it requires C++11 support.
If a class (or struct) doesn't have a constructor, you can initialize it like so:
MyClass a = MyClass();
or
MyClass * b = new MyClass();
This is called value initialization and it usually amounts to zero-initialization.
C++11 gives you initializer_list.
#include <iostream>
struct s
{
int i;
};
int main() {
s s1={666};
s s2={42};
std::cout<<s1.i<<" "<<s2.i<<std::endl;
return 0;
}
You can also do in-class initialization for member.
#include <iostream>
struct s
{
int i=0;
};
int main() {
s s1; //s1.i = 0
//s s2={42}; //fails
std::cout<<s1.i<<" "<<std::endl;
return 0;
}
But you cant do bot at the same time.
It sounds like you are trying to implement the Singleton Pattern. When you do that, you still need a constructor (in fact, if you want to force it to be a singleton, you have to declare the default constructor as private).
class MySingleton
{
private:
// my data
MySingleton() { /* initialize my data */ }
public:
static MySingleton& GetInstance()
{
static MySingleton instance;
return instance;
}
// other functions
};

Are members of a C++ struct initialized to 0 by default?

I have this struct:
struct Snapshot
{
double x;
int y;
};
I want x and y to be 0. Will they be 0 by default or do I have to do:
Snapshot s = {0,0};
What are the other ways to zero out the structure?
They are not null if you don't initialize the struct.
Snapshot s; // receives no initialization
Snapshot s = {}; // value initializes all members
The second will make all members zero, the first leaves them at unspecified values. Note that it is recursive:
struct Parent { Snapshot s; };
Parent p; // receives no initialization
Parent p = {}; // value initializes all members
The second will make p.s.{x,y} zero. You cannot use these aggregate initializer lists if you've got constructors in your struct. If that is the case, you will have to add proper initalization to those constructors
struct Snapshot {
int x;
double y;
Snapshot():x(0),y(0) { }
// other ctors / functions...
};
Will initialize both x and y to 0. Note that you can use x(), y() to initialize them disregarding of their type: That's then value initialization, and usually yields a proper initial value (0 for int, 0.0 for double, calling the default constructor for user defined types that have user declared constructors, ...). This is important especially if your struct is a template.
No, they are not 0 by default. The simplest way to ensure that all values or defaulted to 0 is to define a constructor
Snapshot() : x(0), y(0) {
}
This ensures that all uses of Snapshot will have initialized values.
In general, no. However, a struct declared as file-scope or static in a function /will/ be initialized to 0 (just like all other variables of those scopes):
int x; // 0
int y = 42; // 42
struct { int a, b; } foo; // 0, 0
void foo() {
struct { int a, b; } bar; // undefined
static struct { int c, d; } quux; // 0, 0
}
With POD you can also write
Snapshot s = {};
You shouldn't use memset in C++, memset has the drawback that if there is a non-POD in the struct it will destroy it.
or like this:
struct init
{
template <typename T>
operator T * ()
{
return new T();
}
};
Snapshot* s = init();
In C++, use no-argument constructors. In C you can't have constructors, so use either memset or - the interesting solution - designated initializers:
struct Snapshot s = { .x = 0.0, .y = 0.0 };
Since this is a POD (essentially a C struct) there is little harm in initialising it the C way:
Snapshot s;
memset(&s, 0, sizeof (s));
or similarly
Snapshot *sp = new Snapshot;
memset(sp, 0, sizeof (*sp));
I wouldn't go so far as to use calloc() in a C++ program though.
I believe the correct answer is that their values are undefined. Often, they are initialized to 0 when running debug versions of the code. This is usually not the case when running release versions.
Move pod members to a base class to shorten your initializer list:
struct foo_pod
{
int x;
int y;
int z;
};
struct foo : foo_pod
{
std::string name;
foo(std::string name)
: foo_pod()
, name(name)
{
}
};
int main()
{
foo f("bar");
printf("%d %d %d %s\n", f.x, f.y, f.z, f.name.c_str());
}
I know this question is super old, but this question popped up for me on google and I found this other way and figured I'd add it here:
Snapshot s {};
I'm not sure what C/C++ language version you need for this syntax.

initialize a const array in a class initializer in C++

I have the following class in C++:
class a {
const int b[2];
// other stuff follows
// and here's the constructor
a(void);
}
The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const?
This doesn't work:
a::a(void) :
b([2,3])
{
// other initialization stuff
}
Edit: The case in point is when I can have different values for b for different instances, but the values are known to be constant for the lifetime of the instance.
With C++11 the answer to this question has now changed and you can in fact do:
struct a {
const int b[2];
// other bits follow
// and here's the constructor
a();
};
a::a() :
b{2,3}
{
// other constructor work
}
int main() {
a a;
}
Like the others said, ISO C++ doesn't support that. But you can workaround it. Just use std::vector instead.
int* a = new int[N];
// fill a
class C {
const std::vector<int> v;
public:
C():v(a, a+N) {}
};
It is not possible in the current standard. I believe you'll be able to do this in C++0x using initializer lists (see A Brief Look at C++0x, by Bjarne Stroustrup, for more information about initializer lists and other nice C++0x features).
std::vector uses the heap. Geez, what a waste that would be just for the sake of a const sanity-check. The point of std::vector is dynamic growth at run-time, not any old syntax checking that should be done at compile-time. If you're not going to grow then create a class to wrap a normal array.
#include <stdio.h>
template <class Type, size_t MaxLength>
class ConstFixedSizeArrayFiller {
private:
size_t length;
public:
ConstFixedSizeArrayFiller() : length(0) {
}
virtual ~ConstFixedSizeArrayFiller() {
}
virtual void Fill(Type *array) = 0;
protected:
void add_element(Type *array, const Type & element)
{
if(length >= MaxLength) {
// todo: throw more appropriate out-of-bounds exception
throw 0;
}
array[length] = element;
length++;
}
};
template <class Type, size_t Length>
class ConstFixedSizeArray {
private:
Type array[Length];
public:
explicit ConstFixedSizeArray(
ConstFixedSizeArrayFiller<Type, Length> & filler
) {
filler.Fill(array);
}
const Type *Array() const {
return array;
}
size_t ArrayLength() const {
return Length;
}
};
class a {
private:
class b_filler : public ConstFixedSizeArrayFiller<int, 2> {
public:
virtual ~b_filler() {
}
virtual void Fill(int *array) {
add_element(array, 87);
add_element(array, 96);
}
};
const ConstFixedSizeArray<int, 2> b;
public:
a(void) : b(b_filler()) {
}
void print_items() {
size_t i;
for(i = 0; i < b.ArrayLength(); i++)
{
printf("%d\n", b.Array()[i]);
}
}
};
int main()
{
a x;
x.print_items();
return 0;
}
ConstFixedSizeArrayFiller and ConstFixedSizeArray are reusable.
The first allows run-time bounds checking while initializing the array (same as a vector might), which can later become const after this initialization.
The second allows the array to be allocated inside another object, which could be on the heap or simply the stack if that's where the object is. There's no waste of time allocating from the heap. It also performs compile-time const checking on the array.
b_filler is a tiny private class to provide the initialization values. The size of the array is checked at compile-time with the template arguments, so there's no chance of going out of bounds.
I'm sure there are more exotic ways to modify this. This is an initial stab. I think you can pretty much make up for any of the compiler's shortcoming with classes.
ISO standard C++ doesn't let you do this. If it did, the syntax would probably be:
a::a(void) :
b({2,3})
{
// other initialization stuff
}
Or something along those lines. From your question it actually sounds like what you want is a constant class (aka static) member that is the array. C++ does let you do this. Like so:
#include <iostream>
class A
{
public:
A();
static const int a[2];
};
const int A::a[2] = {0, 1};
A::A()
{
}
int main (int argc, char * const argv[])
{
std::cout << "A::a => " << A::a[0] << ", " << A::a[1] << "\n";
return 0;
}
The output being:
A::a => 0, 1
Now of course since this is a static class member it is the same for every instance of class A. If that is not what you want, ie you want each instance of A to have different element values in the array a then you're making the mistake of trying to make the array const to begin with. You should just be doing this:
#include <iostream>
class A
{
public:
A();
int a[2];
};
A::A()
{
a[0] = 9; // or some calculation
a[1] = 10; // or some calculation
}
int main (int argc, char * const argv[])
{
A v;
std::cout << "v.a => " << v.a[0] << ", " << v.a[1] << "\n";
return 0;
}
Where I've a constant array, it's always been done as static. If you can accept that, this code should compile and run.
#include <stdio.h>
#include <stdlib.h>
class a {
static const int b[2];
public:
a(void) {
for(int i = 0; i < 2; i++) {
printf("b[%d] = [%d]\n", i, b[i]);
}
}
};
const int a::b[2] = { 4, 2 };
int main(int argc, char **argv)
{
a foo;
return 0;
}
You can't do that from the initialization list,
Have a look at this:
http://www.cprogramming.com/tutorial/initialization-lists-c++.html
:)
A solution without using the heap with std::vector is to use boost::array, though you can't initialize array members directly in the constructor.
#include <boost/array.hpp>
const boost::array<int, 2> aa={ { 2, 3} };
class A {
const boost::array<int, 2> b;
A():b(aa){};
};
How about emulating a const array via an accessor function? It's non-static (as you requested), and it doesn't require stl or any other library:
class a {
int privateB[2];
public:
a(int b0,b1) { privateB[0]=b0; privateB[1]=b1; }
int b(const int idx) { return privateB[idx]; }
}
Because a::privateB is private, it is effectively constant outside a::, and you can access it similar to an array, e.g.
a aobj(2,3); // initialize "constant array" b[]
n = aobj.b(1); // read b[1] (write impossible from here)
If you are willing to use a pair of classes, you could additionally protect privateB from member functions. This could be done by inheriting a; but I think I prefer John Harrison's comp.lang.c++ post using a const class.
interestingly, in C# you have the keyword const that translates to C++'s static const, as opposed to readonly which can be only set at constructors and initializations, even by non-constants, ex:
readonly DateTime a = DateTime.Now;
I agree, if you have a const pre-defined array you might as well make it static.
At that point you can use this interesting syntax:
//in header file
class a{
static const int SIZE;
static const char array[][10];
};
//in cpp file:
const int a::SIZE = 5;
const char array[SIZE][10] = {"hello", "cruel","world","goodbye", "!"};
however, I did not find a way around the constant '10'. The reason is clear though, it needs it to know how to perform accessing to the array. A possible alternative is to use #define, but I dislike that method and I #undef at the end of the header, with a comment to edit there at CPP as well in case if a change.