Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ questions

Started by robinei Oct 9, 2000 at 10:06 AM 4 replies 200+ views
Original Post
robinei
robinei
I have a couple of questions about C++: If I am to use "extern CTest Test" in a source file, must "CTest Test" be declared a global variable, or can it be declared withing a class, if the other classes that will use it are declared withinin the same class? I hope you understood that. I know how to overload thhis: "[]". But how can I overload this "[][]"?
BitMaster
BitMaster
You use CTest Test only once in a .cpp file of your choice. The extern CTest Test tells the compiler: There is a variable Test of type CTest somewhere in my project. Take a look at this:
        
// Display.h

class CDisplay {
// ....

};

extern CDisplay Display;

// Display.cpp


CDisplay Display;



This program only has ONE copy of Display that can be accessed by any module that includes Display.h.

About overloading [][]: Not possible, you can only use something like this:
vector< vector < int > > double_array;
That using the vector STL classes, but I hope you know what I mean.

Edit: Corrected templates

Edited by - BitMaster on October 9, 2000 11:36:16 AM
MicahJon
MicahJon

As BitMaster said, directly overloading [][] is not possible, but using multiple vectors is not the only solution.

Have operator [] return a reference to a helper class. The helper class can then override it''s operator []. Then, when you call This[1][2] you get the 1 in This::operator[] and the 2 in Helper::operator[].

    
#include <iostream>

class This; // Forward declaration

class Helper
{
friend class This;
public:
void operator[](int SecondValue)
{
std::cout << "First Value: " << FirstValue << std::endl;
std::cout << "Second Value: " << SecondValue << std::endl;
}
private:
int FirstValue;
};

class This
{
public:
Helper& operator[](int FirstValue)
{
TheHelper.FirstValue = FirstValue;
return TheHelper;
}
private:
Helper TheHelper;
};


int main()
{
This Test;
Test[6][22];


return 0;
}



The one drawback (if it really is) is that Helper must be a member variable of This.

Micah
robinei
robinei
Ok.

Ok but is it really worth going through so much trouble for that.

Isn''t it better putting the array I want to access under public?
Yorvik
Yorvik
as a rule, all class data (ie variables) should go under private, protected is a necessary evil and only in extreme circustances should you plunge into public data mambers.

of corse, if you dont care about correct code, then - ignore me!
MicahJon
MicahJon
Like Yorvik said, traditionally you act paranoid and put everything in private/protected sections. Since I'm not really sure what you're trying to do, I not sure what the best method for you is. However, if all you're doing is accessing elements of an array, I'd probably use a getter function like:

    
class This
{
public:
int Get(int x, int y)
{
return MyArray[x][y];
}
private:
std::vector< std::vector< int > > MyArray;
};


Later,
MicahJon

Edit: Messed my source tags up.

Edited by - MicahJon on October 9, 2000 2:49:10 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.