• ### What is your GameDev Story?

This topic is 4664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
If you define an operator T*, it will be automatically used for array indexing. So you can't define operator[].

##### Share on other sites
Hello,

My first advice: since you are creating a dynamic array class in order to use it in a project, I strongly suggest you to use std::vector<> instead.

Second, your [] overload should either return a T (or const T&) or be non-const. The correct solution might be to write two versions of []:
 const T& operator[] (int i) { return ptrT; } T& operator[] (int i) { return ptrT; }

I would also encourage you to limit the use of automatic casts - despite their apparent convenience, they may have side effects that can be very hard to debug. For example, what will happen if you do this:
CArray<char> string;// ...if (string == "str") {  // do something}

Will you compare the strings of will you compare the pointers?

If you want to gain access to a const version of the internal data, I suggest you to add a little functions (similar to the data() method of std::string).

Regards,

##### Share on other sites
Why are you not using std::vector/deque?, sorry but there is no excuse here, use it and move on. I can already see a deficiency in your custom dynamic array type compared to std::vector.

You can't have an overload of a conversion operator and an overload of subscript operator together and invoke them implicitly without having disambiguates, the compiler has no idea which one to use without more information. You can invoke them explicitly but then it becomes pointless in having an implicit conversion to begin with.

User-defined implicit conversions are nasty in general anyways, they can lead to subtle bugs.

[Edited by - snk_kid on March 30, 2006 3:29:18 AM]

##### Share on other sites
Quote:
 Original post by BariusIf you define an operator T*, it will be automatically used for array indexing. So you can't define operator[].

No, this is just the fault of his compiler. In a compliant compiler, overloading operator[] would be perfectly fine.

I would also avoid overloading casting in that manner anyway though, in favor of being more explicit, but it's up to you.

##### Share on other sites
Quote:
 Original post by snk_kidWhy are you not using std::vector/deque?, sorry but there is no excuse here, use it and move on. I can already see a deficiency in your custom dynamic array type compared to std::vector.

...std::vector is not what i'm looking for... as some time ago my Computer Graphics Teacher at university told me, not all computer game codes are a magnificent example of software perfection (and i have noticed this based on some experience of reading codes from real engines)... if you say reinventing the wheel?... ok it could be but in some cases software engineering "allow" (in a world full of APIs and fourth generation languages...) to make your specific solution to your own well-known problem.

Quote:
 You can't have an overload of a conversion operator and an overload of subscript operator together and invoke them implicitly without having disambiguates, the compiler has no idea which one to use without more information. You can invoke them explicitly but then it becomes pointless in having an implicit conversion to begin with.User-defined implicit conversions are nasty in general anyways, they can lead to subtle bugs.

I agree about, this vector is not at the top of encapsulation, abstraction, or any of the strength form Object Programming Paradigm you want... danger of returning a pointer to the internal data of a class, as Daitel told, is a tricky way of jumping encapsulation, then you take it, assuming consequences, or you leave it... but that the strength of Cpp, up to be an Object Oriented Programming Language you can continue doing low level operations.

##### Share on other sites
Quote:
Original post by Polymorphic OOP
Quote:
 Original post by BariusIf you define an operator T*, it will be automatically used for array indexing. So you can't define operator[].

No, this is just the fault of his compiler. In a compliant compiler, overloading operator[] would be perfectly fine.

I would also avoid overloading casting in that manner anyway though, in favor of being more explicit, but it's up to you.

I agree with you, conceptually the overloading of [] operator must work as an essential cast to a pointer to the first element of the vector... i'm going to tray another compiler than MS VS 6.0... two well-known problems with the compiler, the scope of variable from a for definition and some disadvantage in template uses... ¿God bless Microsoft?... mmmm... who knows???

##### Share on other sites
A friend, as some of you did, suggested me to use only the definition of: "operator const T *() const...", all works perfectly for the:

CArray<char> string;
char *ptrChar = string;

or

CArray<char> to;
CArray<char> from;
strcpy(to, from);

NOTE: only over Linux running GCC compiler (not win - MS VC... yet again Microsoft compiler get involved...)

but... one problem from this sol, it's based on the fact that you can't control the index call over the array, and that is something i'm looking for my class to check... at least on my code, where i will always use [] operator to access elements instead of a direct pointer manipulation (in others code, just trust, like: strcpy call...).

I think i will use a getPtr function or something like that, despite the fact that isn't a very intuitive way... sorry about the delay on answering your messages to my post, i was extremely busy at university!!!

Thanks to all of you for replaying the post...

bye.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634048
• Total Posts
3015237
×