Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

The use of array's in constructor

Posted on 2006-11-18
5
Medium Priority
?
227 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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