Toon posts:

[C++] Template van één functie in een classe

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik heb het volgende probleem:

Ik wil graag door middel van één template functie of een int of een String opvragen.
dus door middel van getAttribute<int>("test"); Dat ie dan de int waarde uit de private map, 'intAttributes' returned.
En als ik zeg getAttribute<String>("testje"); Dat ie dan de String waarse uit de private map, 'strAttributes' returned.

Ik vraag me dus af of dit mogelijk is. En zo jah hoe.
De onderstaande code heb ik nu, maar als ik dit compile zegt ie: "Qualifier 'EntryCall' is not a class or namespace name"


Ik hoop dat dit duidelijk genoeg is zodat iemand mij kan helpen :)
EntryCall.h:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <map.h>
class EntryCall {
  private:
    map<String, String> strAttributes;
    map<String, int> intAttributes;

  public:
    EntryCall();

    void setAttribute(String naam, String value);
    void setAttribute(String naam, int value);

    template <class value_type>
    value_type getAttribute(String naam);
};
#include "EntryCall.cpp"



EntryCall.cpp:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Default constructor
EntryCall :: EntryCall() {
  //initialiseer set

}

//Put attribute value in integer - set
void EntryCall :: setAttribute(String naam, int value) {
  intAttributes[naam] = value;
}

//Put attributes value in String - set
void EntryCall :: setAttribute(String naam, String value) {
  strAttributes[naam] = value;
}

template <typename value_type>
value_type EntryCall :: getAttribute(String naam){
  return intAttributes[naam];
}

  • Zoijar
  • Registratie: September 2001
  • Niet online

Zoijar

Because he doesn't row...

Je include natuurlijk nooit een .cpp file :? Haal regel 16 is weg. En dan meteen die spaties voor en na :: :)

En verder...hier wil je helemaal geen templates voor gebruiken lijkt me. Gewoon 2 functies getIntAtt() en getStrAtt().

Als je perse templates zou willen gebruiken, kom je toch op 2 template specialisaties, en 1 algemene die een error geeft. Beetje omslachtig.

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Waar jij hier in feite op doelt, is methode selectie op basis van het return type van een methode en het verwachtte type van een expressie (de context). Dit wordt ook wel "overloading based on return type" genoemd.

Je wilt een methode in feite op verschillende manieren implementeren. Als deze variantie plaats vind op basis van een parameter is er geen probleem: je kan de methoden dan overloaden. Als deze variatie echter niet op een parameter, maar op het return type plaats moet vinden ben je flink de pineut in vrijwel alle talen (behalve bijvoorbeeld Ada) en moet je in de methodenaam opeens het type opnemen.

Vervelend, maar het is nu eenmaal zo :) . Overloading is tenslotte toch een vorm van ad-hoc polymorfisme, dus zo'n ramp is het niet ;) .

Ik vond zo snel dit linkje:
http://cpptips.hyperformix.com/cpptips/overl_ret_types2

en deze:
http://plg.uwaterloo.ca/~rcbilson/cforall/overload.html
The problem of overload resolution has been widely studied with respect to popular imperative programming languages such as Ada, C++, and Java. These languages allow overloading in different situations, which imposes different requirements on the resolution algorithm. Ada allows overloading based on argument and/or return types, but has no implicit conversions. C++ and Java only allow overloading based on argument types, but have a rich set of implicit conversions that can potentially be applied to actual parameters. Ada allows overloading of functions, operators, and enumerated constants; C++ allows overloading of functions and operators, and Java only allows overloading of functions.

Java and C++ algorithms for resolving overloads are insufficient for Ada, since the potential for overloading based on return type adds significant complexity. This lead to algorithms by Baker[1] and Cormack[2], which incorporate both arguments and return types.

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment