Arrays and constructors

A

andy

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?
 
H

hardi.pertel

andy said:
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?

The constructor is only called when you create a variable of type
Shape. But maybe it's possible to access those variables (vertices)
when they are static
they will be erased if they are not static(this is your only chance)
 
R

Rolf Magnus

andy said:
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).

The above code doesn't do that. It creates within the Shape constructor an
array of three instances of OtherClass and calls that array 'vertices'.
My question is how do access the contents of the array (vertices[0],
vertices[1], vertices[2]) within the main() function.

The array is local to the constructor. It ceases to exist once the
constructor returns.
 
R

Ron Natalie

andy said:
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).

No it doesn't it creates a three element array of OtherClass (default
constructed) local to the Shape constructor functino.
My question is how do
access the contents of the array (vertices[0], vertices[1],
vertices[2]) within the main() function.

You can't. vertices is a local variable. As soon as Shape's
constructor returns it is gone.

If you want an array to be CONTAINED inside Shape, you should
declare it there. For example:

class Shape
OtherClass vertices[3];
public:
Shape(); // your defaut constructor
};
This is code that I currently have:

cout << myShape.get_value();

You could write one to return the internal object. However,
you're going to have to tell me what it is that you are trying
to do. What do you want output? If you feed vertices to
cout<< it's just going to convert it to void* and print the
address ( not very interesting).
 
S

Salt_Peter

Thus making it static could help to access it later

Making it static would destroy the project. Thats like saying that all
and any points share the same 3 coordinates regardless of location. And
besides, static does not "help" access, it complicates it.
 
S

Salt_Peter

andy said:
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];
}

The above creates a temporary array. You could argue that it doesn't
exist and i wouldn't be surprised that the compiler optimizes it away.
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?

I think you are barking up the wrong tree. A 3D point can be used to
create a vertex. A shape can hold a container of vertexes - say
std::vector< Vertex > (ie: a triangle has 3 of these). But an abstract
shape does not have a fixed number of vertices. It seems to me that
what you are describing above is a Point3D, not a Shape.

And if what you are planning to argue, insofar as the Shape is
concerned, is its coordinate location:

I'm seeing something like so:

#include <iostream>
#include <iterator>

class Point3D {
double x;
double y;
double z;
public:
Point3D(double x_ = 0.0, double y_ = 0.0, double z_ = 0.0)
: x( x_ ), y( y_ ), z( z_ ) { }
friend std::eek:stream&
operator<<( std::eek:stream& os, const Point3D& r_pt)
{
os << "pt.x = " << r_pt.x;
os << " pt.y = " << r_pt.y;
os << " pt.z = " << r_pt.z;
return os << std::endl;
}
};

class Shape {
Point3D location;
public:
Shape() : location() { }
Shape( const Point3D& loc ) : location( loc ) { }
void move( const Point3D& loc ) { location = loc; }
const Point3D getLocation() { return location; }
};

int main()
{
Shape shape;
Point3D pt = shape.getLocation();
std::cout << pt;

shape.move(Point3D(1.0,1.0,0.0));
std::cout << shape.getLocation();

return 0;
}

/*
pt.x = 0 pt.y = 0 pt.z = 0
pt.x = 1 pt.y = 1 pt.z = 0
*/
 
A

andy

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

=?ISO-8859-15?Q?Juli=E1n?= Albo

andy said:
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.
(...)

//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 ?

Forget optimization until you know some more C++. You can't have a clue
about optimizing array usage if you don't know how to use an array.
 
S

Salt_Peter

andy said:
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 ?

Yes, its clearer except that you've missunderstood one fundamental
isse, with a std::vector, the code would be both much easier to
implement and (surprise!) a std::vector is faster than a primitive
array.

I can achieve the entire code below with 1 line in main using a
std::vector. Yes: just one line.
std::vector< double > vn(11.1, 5);
Except, of course, that the vector is dynamic, resizeable, reserveable.

Note that you can't template Class1 if you choose to have
floating-points because a floating literal constant can't be in a
template parameter list. Something else a std::vector has no issues
with.

#include <iostream>
#include <stdexcept>

class Class1 {
double d; // modify to your convenience
public:
Class1() : d(11.1) { // default - set it to whatever you need
std::cerr << "Class1()\n";
}
friend std::eek:stream&
operator<<( std::eek:stream& os, const Class1& r_c1 ) {
return os << r_c1.d;
}
};

// T is your target type
// Size is the array's injected size
template < typename T, const size_t Size >
class Container {
T array[ Size ];
public:
Container() { }
// size_t size() const
size_t size() const {
return Size;
}
// at(index)
T& at( const size_t index ) {
if ( Size <= index ) // bounds check
throw std::range_error(
"Container index out of range."
);
return array[ index ];
}
};

int main() {
try {
Container< Class1, 5 > container;

for ( size_t i = 0; i < container.size(); ++i ) {
std::cout << "container[" << i << "] = ";
std::cout << container.at( i ); // using accessor at()
std::cout << std::endl;
}

} catch ( const std::exception & r_e ) {
std::cerr << "error: ";
std::cerr << r_e.what() << std::endl;
}
}

/*
Class1()
Class1()
Class1()
Class1()
Class1()
container[0] = 11.1
container[1] = 11.1
container[2] = 11.1
container[3] = 11.1
container[4] = 11.1
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top