answersLogoWhite

0


Best Answer

You can initialise a structure's data members in both C and C++.

In C, you initialise the structure at the point of instantiation.

typedef struct foo

{

int m_data1;

double m_data2;

} foo;

int main()

{

foo f={.m_data1=42, .m_data2=1.99};

return 0;

}

You can also assign members separately from instantiation, but this is considered bad coding practice as all the members will be initialised with garbage (whatever happens to reside in memory at the point of instantiation). This raises the potential for accessing that garbage data before it is assigned.

foo f;

/*

any attempt to access f's members at this point

will result in undefined behaviour

*/

f = (foo) {.m_data1=42, .m_data2=1.99};

/* f is now initialised */

In C++, a struct is exactly the same as a class except it has public access by default, rather than private access by default. As such, you can use the class constructor initialisation list to initialise the data members to default values:

struct foo

{

foo():m_data1(42),m_data2(1.99){}

private:

int m_data1;

double m_data2;

};

Typically, you will also provide an overloaded constructor to initialise the members to more specific values:

foo(const int data1, const double data2): m_data1(data1), m_data2(data2){}

However, you can also combine the default constructor and the overloaded constructor to produce an overloaded default constructor:

foo(const int data1=42, const double data2=1.99): m_data1(data1), m_data2(data2){}

You can also implement a copy constructor with an initialisation list:

foo(const foo& f):m_data1(f.m_data1),m_data2(f.m_data2){}

Note that initialisation lists are more efficient than using the constructor body to perform initialisation. Therefore you should always perform as much initialisation as possible in the initialisation list and only use the constructor body for any additional initialisation processes that cannot be handled by the initialisation list. Ideally, members should be initialised in the same order they were declared.

Note also that the initialisation list employs construction syntax rather than assignment syntax, even for primitive data types. If the class or structure is a derived class, you can also invoke specific base class constructors through the initialisation list. Again, these base classes should be listed in the same order specified by the inheritance list, and should be listed BEFORE the data members since the base classes must be constructed first.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why can't you initialize data members in a structure in c or c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is it mandatory to declare the structure variable of a structure to access the data members?

true or false


What is copy constructor in object oriented programming?

The purpose of constructor in object oriented programming is to initialize data. Likewise copy constructors are used to initialize an object with data from another object.


How you can initialize an array with data values during declaration?

int myArray[] = {1,2,3,4,5};


What are public data members in a class?

Public data members are akin to structure data members (which are public by default). Since they are public, they are fully exposed outside of the class. Data validation becomes impossible. Even if you define an interface, there's no requirement to use it since the members are readily available. If the members must be validated, do not assign them public access.


When class will be used and when structure will be used?

In class default members are private and in structure default members are public ,When ever you want to hide data from outside functions then you can use class.But in ANSI C we can hide data by using private access specifier.


Why cannot initialize data member within class?

A class is a type. Classes don't do anything except define the type. You have to instantiate an object of the type in order to actually do anything, including initialising data members. However, the class can define how a data member is initialised. The most efficient method of initialising class members if via the class constructor initialisation list.


What are the goals of data structure?

To arrange data in a particular manner.these manner or set of rules is defined in the data structure so that the data used in computer systems can properly used at necessary time.


What is an association list?

An association list is a data structure which associates keys with data, implemented as a list whose members are paired storage locations.


Describe various members a class definition may contain?

Fields - the various variables and objects your class uses, Constructors - used to initialize instance varriables, Methods - allow you to retrieve and manipulate data in an object


What are the subject-matters of data structure?

types of data structure types of data structure


What is meant by structure in c?

A structure is an aggregate of two or more data types, each of which has a unique address within the structure. The structure members need not be the same type. Structures are akin to a database record where members represent the record's fields. The size of a structure is not necessarily equal to the sum of its member sizes. For efficiency, members are typically aligned on an appropriate word boundary which may introduce gaps within the structure. To minimise these gaps, it is best to declare members in order of size, largest first.


How do you amend a data structure?

How do you amend a data structure?