calling a constructor within a constructor

B

Brett Irving

Hi once again, another question,

I want to make a class object MyArray a global but have no idea about
how to go about initialising it within the constructor as it wont let
me do something like,

private:
int size;

MyArray MyArr(size);

I think I need to call the class constructor something to the effect
of

yourArray(int s, int num): MyArr(s)
{
............
}



My code is similar to this

class MyArray
{
............
MyArray(int size);

}


class yourArray
{

private:
MyArray(int);

public:
..
..
..
..
..
}


please help.
 
R

Rolf Magnus

Brett said:
Hi once again, another question,

I want to make a class object MyArray a global but have no idea about
how to go about initialising it within the constructor as it wont let
me do something like,

private:
int size;

MyArray MyArr(size);

I think I need to call the class constructor something to the effect
of

yourArray(int s, int num): MyArr(s)
{
...........
}



My code is similar to this

class MyArray
{
............
MyArray(int size);

}


class yourArray
{

private:
MyArray(int);

public:
.
.
.
.
.
}

Please provide a minimal, but _complete_ example program of what you are
trying. The above code fragments don't make sense.
 
D

David White

Brett Irving said:
Hi once again, another question,

I want to make a class object MyArray a global but have no idea about
how to go about initialising it within the constructor as it wont let
me do something like,

private:
int size;

MyArray MyArr(size);

I think I need to call the class constructor something to the effect
of

yourArray(int s, int num): MyArr(s)
{
...........
}



My code is similar to this

class MyArray
{
............
MyArray(int size);

}


class yourArray
{

private:
MyArray(int);

Make it:
MyArray myArray;

where 'myArray' is just a member variable name of your choice.

Your constructor will look something like:

yourArray::yourArray(int s, int num) : myArray(s) {}

However, this will not make it "global" as you requested. A MyArray object
will be in every yourArray object.

David
 
J

John Harrison

Brett Irving said:
Hi once again, another question,

I want to make a class object MyArray a global but have no idea about
how to go about initialising it within the constructor as it wont let
me do something like,

You should stop using the word global to mean what every one else calls a
member variable.

And you should post more complete code.

john
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top