Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overloading - Constructor 1

Status
Not open for further replies.

UnityKing

Programmer
Dec 12, 2004
17
0
0
CA
Simple question, Hard Awnser :

1) Seg is my class

class seg {
... nanan anan anan
}


2) i do

seg Dot[20];



i want to have 20 in a variable
i tryed [] overloading and didnt work
and constructor wont be usefull ( seg Dot[20] = {20,20,20,20,20, etc...})

so how coud i get the Bound of the array when its declared.


What i want to do,
im just a c++ newbie (im good in vB), i want to make a graph (working on the shortest path algorthim)
since the max number of segments that start from a point cant be larger than the Number of Plot - 1 i want to declare a Dynamic array with will be the optimal size ( int *Segment, Segment = new int [NumberOfPlot-1] ). Got it ?...no ok well if u understand me or not just add a reply ...please

Thanks
Guillaume
 
i want to have 20 in a variable
What's this variable? You have array of 20 class seg variables. They are not integers.
Make seg::seg(int) constructor then initialize seg[] with {20, 20, ... }.
i tryed [] overloading...
Why?
 
A graph is an array of nodes, a node to another node u have a segment. I want to delacre a graph, with will contain an array of POSSIBLE link. supose that u have 20 nodes in a graph, there is (NBOFNODES - 1) possibles links, since a link to it self is not really important.

Im trying to overloard, or initalise a class with will the proper possibles number of segments. theres the class :


class seg{

int *Links;
seg()
~seg()
};

seg::seg(){

Links = new int [(*This->Ubound)-1] // Dynamic array the size of the array of the class

}
seg::~seg(){

Delete [] Links
}


Voila i just want to know how to get - *this->UBOUND -
 
What is *this->UBOUND supposed to do?
You don't have a UBOUND member in your class, all you have is Links...

If I'm understanding correctly (after a lot of assumptions), you want to create an array of seg objects and you want each seg object to know how many seg elements are in the array right?

If that's the case, one way I think might work is to use a named constructor idiom.
 
THANKS :p, thats what i want. I want to encapsulate the Size of an array IN the class that have been declared array
 
No no no


look


i want to know how many time i declared an array.

cat Cuty[4];

i want a variable in my class that will contain the number 4 because i have 4 cats !

so i will be hable to do :
Cuty.Ubound() // witch will give the number 4 (Upper Bound)
 
No. An array is not a class, it's just a sequence of objects, therefore it has no functions like Ubound()... You should probably consider using a vector.
Code:
std::vector<seg> Dots;
seg Dot;
Dots.push_back( Dot );    // Add as many segs as you want.
int ubound = Dots.size();
 
Look like its what i want, thanks ;-), is it optimal ?
 
That depends on what you mean by optimal, but I'd say it is the best solution since the STL classes have been tested and refined by thousands of programmers and are designed to be fast, small and portable.
You should write your program correctly with tested & proven code like STL first, then and only then, if you find that you really have a performance issue, you can start fine tuning your code.
 
Shoud i use Vector to represent a graph ?

im working on a shortest patth algorithm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top