overloading operator[] in addition with templates

Started by
5 comments, last by ChaosEngine 10 years ago

Hi,

I am in trouble with some of my code. I try for a couple of days to implement an overloaded template method. But it doesn’t runs.

I try to integrate the overloaded template method “T &operator[] (int i)”, you can see it below.

template <class T>

class List

{

private:

class ListNode

{

public:

ListNode *previousNode;

ListNode *nextNode;

T content;

} *node;

ListNode *firstNode;

ListNode *lastNode;

int nodeCount;

public:

...

T &operator[] (int i)

{

return firstNode->content;

}

// <TR> WORKAROUNG: use operator overloading

T getContent(int Index)

{

ListNode *currentListNode = getListNodeAtIndex(Index);

return currentListNode->content;

}

};

#include <iostream>

#include "core.h"

#include <string>

using namespace std;

int main()

{

List<string> *lst = new List<string>();

....

for (int i = 295; i < 305; i++)

{

//lst->getContent(i) = "Test";

string test = lst;

}

system("PAUSE");

return 0;

}

If I use “T getContent(int Index)”, It runs. But If I use “T &operator[] (int i)”, IntelliSense says following: “2 IntelliSense: no suitable user-defined conversion from "List<std::string>" to "std::string" exists c:\development\meteo\application\Application.cpp 26 17 application”

I don’t know why he says this. Also I tried moving T &operator[] (int i)

Into the ListNode class, but this doesn’t runs, too.

I don’t know, if it is important for you, but I want to store this in a DLL File.

I hope everyone can help me. I am on the way to despair.

I hope you can understand me, my English is not the best.

Yours sincerely

Dennis Köhler aka Pixma

So long Pixma
Advertisement
Dereference lst before accessing: string test = (*lst); Otherwise, you're treating lst as an array of List<string>s and accessing the ith list in lst.
Also, lst doesn't need to be dynamically allocated. If you List<string> lst; then the original code should work.

Your problem is that lst is a pointer in this code.


List<string> *lst = new List<string>();
string test = lst[i]; 

Pointers have a built in [] operator which is essentially address + index (simplifying here).
Change it to


List<string> lst;
//....
string test = lst[i]; 

and it will work.

Also, you do realise you don't need this class, right? If you're doing it for learning purposes, that's cool, but don't use it in "real code".

Use the standard library.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

thanks you both for your help.

Hmm, Why should I don't use it in my real code.

I think the positive arguments are

- Total control

- Opportunities foroptimization

- Customizability

I read this in Chapter five of Jason Gregory's book "Game Engine Architecture".

This are the reasons, why I want to develope my own container classes.

I think Crytek, Ubisoft, etc. does this in the same way like I, doesn't they?

So long Pixma

I think Crytek, Ubisoft, etc. does this in the same way like I, doesn't they?

Well, they almost certainly are not implementing a random access container as a doubly linked list with an accidental additional member. They would not be heap allocating such an object unnecessarily. Such teams don't write custom containers for theoretical benefits, these containers would be born from trying to solve practical problems with existing alternatives. And obviously, almost all programmers on the team would not be writing their own custom containers - they would have to use the existing custom container that already exists in the project infrastructure.

Do not mistake the habits of large professional teams for ones that everyone should follow all the time. A good programmer evaluates each thing they do in the context of what makes sense for the project they're working on.

Developing your own containers is an invaluable learning experience, but most professionals would agree that you should not use your own containers for production code, there are just too many high quality existing implementations that are free of bugs, already optimised and already as customisable as a container could be. The ones in your Standard C++ Library are excellent implementations given the constraints of the language specification.

ok, you have convinced me. I finish my List class for a learning experience and use then the Standard C++ Library.

So long Pixma


Developing your own containers is an invaluable learning experience, but most professionals would agree that you should not use your own containers for production code, there are just too many high quality existing implementations that are free of bugs, already optimised and already as customisable as a container could be. The ones in your Standard C++ Library are excellent implementations given the constraints of the language specification.

Essentially this. Developing your own containers to fix problems you have identified with the SC++L is an expert level task.

Only do it if:

  1. you absolutely have to
  2. you really REALLY know what you're doing.

Usually the kinds of people who do this are intimately familiar with their platform.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement