473,383 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Arrays and constructors

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?

Nov 18 '06 #1
9 2206

andy wrote:
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)

Nov 18 '06 #2
andy wrote:
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.

Nov 18 '06 #3
The array is local to the constructor. It ceases to exist once the
constructor returns.
Thus making it static could help to access it later

Nov 18 '06 #4
andy wrote:
>
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).
Nov 18 '06 #5

ha**********@gmail.com wrote:
The array is local to the constructor. It ceases to exist once the
constructor returns.

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.

Nov 18 '06 #6

andy wrote:
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::ostream&
operator<<( std::ostream& 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
*/

Nov 18 '06 #7

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 ?

Nov 19 '06 #8
andy wrote:
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.

--
Salu2
Nov 19 '06 #9

andy wrote:
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::ostream&
operator<<( std::ostream& 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
*/

Nov 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
3
by: Andrew.Morgan | last post by:
Hi I have three classes TDigest, THash and TSHA1. //--------------------------------------------------------------------------- class TDigest { public: // Constructors. TDigest(int Count);
5
by: Andy Fish | last post by:
Hi, I have a HTML page with javascript in it which pops up another HTML page. I can pass simple variables fairly freely between the two pages. I can pass objects between them two, and I have...
3
by: Alexander Malkis | last post by:
class A { B b; public: A() { }; //... }; Question: are the elements of b get initialized to zero during the call A();
46
by: Blue Ocean | last post by:
How do I do it? I'm doing a practice problem which includes me implementing a set of ints as a class. I don't want to use vector because that would be cheating. ;) I don't want to spend...
2
by: Todd Smith | last post by:
I have this code, but I get a runtime error that says arr isn't set to an instance of a class. So how do I initialize an array of objects, calling all the constructors for each thing in the array?...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
2
by: Colin McGuire | last post by:
Hi everyone. This is a question and something new and neat I have found. But to recap - a few months back I learnt about overloading, how to create the same method with a different signature. With...
19
by: Jim West | last post by:
The execution speed of the following code is dramatically faster if I declare some arrays globally rather than locally. That is FOO a, b, c; void bar() { ... } runs much faster (up to...
19
by: John Whitworth | last post by:
Hi, Firstly, apologies if what I am asking seems really obvious. I'm a self-taught VB2008 (via VB4, VB6 and VB2003) user, and have always just picked up what I need to know, when I need to know...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.