Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

The use of array's in constructor

Hello,

I have created a class called shape, I've then called the following code:

shape myShape;

This then calls the default constructor for this class:

            Shape::Shape( void )
            {
                  OtherClass vertices[3];
            }

From now on I'm guessing what should be happening.  This code passes an array called vertices to the default constructor of OtherClass(this constructor simple sets a few double variables).  My question is how do access the contents of the array (vertices[0], vertices[1], vertices[2]) within the main() function.

This is code that I currently have:

cout << myShape.get_value();

Any suggestions?
0
andyw27
Asked:
andyw27
1 Solution
 
jkrCommented:
>>My question is how do access the contents of the array (vertices[0], vertices[1], vertices[2]) within >>the main() function.

That way: Not at all. The array is "local" to the constructor, i.e. will be removed from memory when the constructor exits. You will have to make that array a class member in order to be able to access it, e.g.

class Shape {

//...

public:

OtherClass& get_value(unsigned int index) { return vertices[index];}

private:

OtherClass vertices[3];
};

int main() {

Shape shape;
OtherClass vertex = shape.get_value(1);

}

That is just an outline, you should check wheter the index value is within the array bounds to not access memory that is not "yours".
0
 
numansiddiqueCommented:
Your question in not very clear.
In the constructor of the Shape class you have
OtherClass vertices[3];

i think vertices is an object array of class OtherClass. I think you cannot access the vertices array object from the main function because vertices array object goes out of scope after the constructor of the Shape class finishes its execution.

may be you can do something like this

Class Shape
{
public:
OtherClass vertices[3];
..
..
..
};

if the vertices array object is public you can access it using
shape.vertices[0];

if its declared private you cannot access from outside the class
0
 
andyw27Author Commented:
The under lining reason why I need to do this is to utilize the
constructor of one class in a second class - thereby optimizing the
code by avoiding duplication.

Take this example:

Constructor of first class:

Class1::Class1( void )
{
        a = 0;

}

Instead of duplicating this I would like Class2 to use this same
constructor:

Class2::Class2( void )
{
        //I would like code here that would pass an array to the Class1
constructor and if the array was 5 in size then each position in the
array would be initialized with a=0

}

I hope this is a little clearer ?
0
 
jkrCommented:
Yes, a little bit - but there seems to be a misconception, since the constructors are called automatically anyway.
0
 
itsmeandnobodyelseCommented:
>>>> I need to do this is to utilize the constructor of one class in a second class

You can do such a thing only if class 1 and class 2 are somehow related, e. g. class 2 is derived from class 1

class class2 : public class1
{
    ...  // now class2 "is a" class1 and shares the members of class1
};

or class 2 has an object of class 1 as a member

class class2
{
     class1 m_c1;   // class 2 "has a" class1 object
     ...                   // and may access it via public member functions of class1
};
 

Both approaches always make sure that you have a *pair* of one class2 object and one class1 object which are associated and therefore may share some data.

Your approach:

  Class1::Class1( void )
  {
        a = 0;

  }

doesn't give a hint *which* instance of Class2 should share any data with the currently generated Class1 object. Let's assume 'a' is some global variable defined somewehere else. Even then, there isn't any Class2 object available when constructing Class1. Moreover, all instances of Class1 would refer to the same 'a' what hardly makes sense beside Class1 would be a singleton class (with one instance only).

Regards, Alex


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now