Solved

The use of array's in constructor

Posted on 2006-11-18
5
216 Views
Last Modified: 2010-04-01
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
Comment
Question by:andyw27
5 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
 
LVL 2

Expert Comment

by:numansiddique
Comment Utility
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
 

Author Comment

by:andyw27
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yes, a little bit - but there seems to be a misconception, since the constructors are called automatically anyway.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
Comment Utility
>>>> 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now