Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


overloading operator[] in addition with templates


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Pixma   Members   -  Reputation: 131

Like
0Likes
Like

Posted 14 April 2014 - 03:31 PM

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


So long Pixma

Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 4474

Like
1Likes
Like

Posted 14 April 2014 - 03:53 PM

Dereference lst before accessing: string test = (*lst)[i]; 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.
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#3 ChaosEngine   Crossbones+   -  Reputation: 2503

Like
2Likes
Like

Posted 14 April 2014 - 03:58 PM

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

#4 Pixma   Members   -  Reputation: 131

Like
0Likes
Like

Posted 14 April 2014 - 04:06 PM

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

#5 rip-off   Moderators   -  Reputation: 8762

Like
5Likes
Like

Posted 14 April 2014 - 04:35 PM

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.

Edited by rip-off, 14 April 2014 - 04:35 PM.


#6 Pixma   Members   -  Reputation: 131

Like
2Likes
Like

Posted 14 April 2014 - 04:47 PM

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


So long Pixma

#7 ChaosEngine   Crossbones+   -  Reputation: 2503

Like
1Likes
Like

Posted 14 April 2014 - 05:14 PM


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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS