Solved

The use of array's in constructor

Posted on 2006-11-18
5
225 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
[X]
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
5 Comments
 
LVL 86

Expert Comment

by:jkr
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 {

//...

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


0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

623 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