Sign in to follow this  

overloading operator[] in addition with templates

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i];

       }

       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

Share this post


Link to post
Share on other sites
Dereference [tt]lst[/tt] before accessing: [tt]string test = (*lst)[i];[/tt] Otherwise, you're treating [tt]lst[/tt] as an array of [tt]List<string>[/tt]s and accessing the [tt]i[/tt]th list in [tt]lst[/tt].
Also, [tt]lst[/tt] doesn't need to be dynamically allocated. If you [tt]List<string> lst;[/tt] then the original code should work.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites
Sign in to follow this