The use of array's in constructor

Posted on 2006-11-18
Last Modified: 2010-04-01

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?
Question by:andyw27
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 17972055
>>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 {



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


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".

Expert Comment

ID: 17972059
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
OtherClass vertices[3];

if the vertices array object is public you can access it using

if its declared private you cannot access from outside the class

Author Comment

ID: 17974163
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

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 ?
LVL 86

Expert Comment

ID: 17975118
Yes, a little bit - but there seems to be a misconception, since the constructors are called automatically anyway.
LVL 39

Accepted Solution

itsmeandnobodyelse earned 500 total points
ID: 17986391
>>>> 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


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
simple way to manipulate chinese characters in non unicode mfc c++ application. 9 252
Grammars for C C++ and java 1 145
How to split this in C++ 4 129
Least Squares Curve Fitting 4 133
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question