will memory be reserved for this object?

G

Good Guy

I have following two classes:

template <size_t size>
class Cont{
public:
char charArray[size];
};
template <size_t size>
class ArrayToUse{
public:
Cont<size> container;
inline ArrayToUse(const Cont<size+1> &
input):container(reinterpret_cast<const Cont<size> &>(input)){}
};

I have two following lines of code at global scope:

const Cont<12> container={"hello world"};
ArrayToUse<11> temp(container);
char (&charArray)[11]=temp.container.charArray;

In totality of my code The only usage of "container" object is for
initialization of an object of "ArrayToUse" class as mentioned and
after initialization of "charArray" reference to
"temp.container.charArray" I'll use that reference in rest of my code,
now I'm wondering whether after building the code memory will be
reserved for "container" object or not since that's got a temporary
usage.
 
H

Helge Kruse

Good Guy said:
I have two following lines of code at global scope:

const Cont<12> container={"hello world"};
ArrayToUse<11> temp(container);
char (&charArray)[11]=temp.container.charArray;

In totality of my code The only usage of "container" object is for
initialization of an object of "ArrayToUse" class as mentioned and
after initialization of "charArray" reference to
"temp.container.charArray" I'll use that reference in rest of my code,
now I'm wondering whether after building the code memory will be
reserved for "container" object or not since that's got a temporary
usage.

Since it's a global object, the object's life time is not limited.

You can define in any other compilation unit "extern const Cont<12>
container;" and reference this object. The compiler cannot know this while
compiling the compilation unit where you define container, temp and
charArray.

Helge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top