-
April 1st, 2006, 02:13 PM
#1
overloading operator + another problem
i have two classes. class Ticket and class Game. A game consist of an array of digits and a Ticket consist of a number of games. Two games mustn't be equal eg 1 2 3 and 4 5 6.
In my Ticket class i've created a pointer called ticketGame and this pointer points to an array of Games. so that means that
*(ticketGame + i ) = a game
Is it possible to access a specific index in a specific *(ticketGame + i )???
Eg can i say ticketGame[1][2]???
and how should i overload my = operator to be able to do
Code:
*(ticketGame + i ) = game1;
-
April 1st, 2006, 02:21 PM
#2
Re: overloading operator + another problem
Well *(ticketGame +i) is the same as ticketGame[i]. The assignement should work if your game is assignable, as ticketGame[i] is a lvalue -- or should be one.
However, I think you miss some basics here -- no offense intended.
-
April 1st, 2006, 02:49 PM
#3
Re: overloading operator + another problem
you are absolutely right as i've just started learning c++. Thanks for the advise. I'll try to improve.
-
April 1st, 2006, 05:55 PM
#4
Re: overloading operator + another problem
Why keep a vector of pointers? And why not vector of objects? That would be much safer and you would not need to write the memory management for the Ticket class. Here is a sample that I wrote.. might not be exactly what you are wanting but tells you how to proceed using vector of objects and not pointers.
Code:
#include<iostream>
#include<vector>
#include<algorithm>
class Game{
private:
std::vector<char> vector_of_digits; //if you want to store digits - you could even internally use std::string for this
//std::vector<int> vector_of_integers; //if you want to store integers and not just digits
//other private members
public:
Game(char a, char b, char c){
vector_of_digits.push_back(a);
vector_of_digits.push_back(b);
vector_of_digits.push_back(c);
}
//accessors/ modifiers/overloaded constructors/ any other members
bool operator==(const Game& rhs) const{
//this logic would return true even if a single member of both arrays are found common irrespective of their order.
//loop through each element of this->vector to see if it exists in the vector of rhs
//if yes - return true else return false
for(size_t i = 0 ; i < vector_of_digits.size() ; ++i){
std::vector<char>::const_iterator it = std::find(rhs.vector_of_digits.begin(), rhs.vector_of_digits.end(), vector_of_digits[i]);
if (it != rhs.vector_of_digits.end()){
return true;
}
else
{
continue;
}
}
return false;
}
friend std::ostream& operator<<(std::ostream& os, const Game& obj);
};
std::ostream& operator<<(std::ostream& os, const Game& obj){
std::cout << "show game" << std::endl;
for(size_t i =0; i < obj.vector_of_digits.size(); ++i)
os << obj.vector_of_digits[i] << std::endl;
return os;
}
class Ticket{
private:
std::vector<Game> vector_of_games;
public:
void Add(const Game& obj){
//before adding a game object into the internal games vector/array
//check if one exists that violates the equality restriction you defined.
std::cout << obj << "inside Add" << std::endl;
std::vector<Game>::const_iterator it = std::find(vector_of_games.begin(), vector_of_games.end(), obj);
if (it==vector_of_games.end())
{
//add the new distinct Game object
vector_of_games.push_back(obj);
std::cout << "Added Game successfully into Ticket " << std::endl;
}
else{
std::cout << "Game already exists ... cannot add duplicate\n" ;
//ideally raise an exception that the element cannot be added - violates duplicate rule..or whatever that you call it.
}
}
};
int main(){
Ticket ticket_object;
ticket_object.Add(Game('1','2','3')); //should get added cause this is the first one into the empty vector of Ticket
ticket_object.Add(Game('4','5','6')); //should get added because all digits of previous game added are different from this one
ticket_object.Add(Game('2','8','9')); //should not get added into the Ticket class underlying vector
return 0;
}
The output on VC++ 6.0 that I got was:
Code:
show game
1
2
3
inside Add
Added Game successfully into Ticket
show game
4
5
6
inside Add
Added Game successfully into Ticket
show game
2
8
9
inside Add
Game already exists ... cannot add duplicate
As for the operator[][] - see this FAQ - C++ FAQ Lite - Operator overloading. It is suggested to use operator() instead of operator[][]. Hope this helps. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|