I am starting to experiment with templates, having accepted the messy inconvenience of having to both declare AND defining the functions within the same header file.
One problem I have encountered is how to declare and define a member function that returns or accepts as an argument an object of its own type. What I mean is, by example of a non-template class:
Specifically the difference being - aside from prefixing every member function definition with 'template<typename dataType>' - that every time 'tCArray' is used to declare an argument or return-type it must be given as 'tCArray<typename dataType>'.
Have I got the right end of the stick? Because that feels incredibly cumbersome, but if I try to compile without doing so I get errors.
MANY thanks MiiniPaa! I am working from the 'C++ The Complete Reference, 4th Edition' and sadly Schildt doesn't give any examples of returning templates object types or using them as arguments!
Could you explain to me why you need the <dataType> for the return value but not the argument?
I have learned to avoid StackOverflow - I did post there a bit at one time but it is such a poisonous, unfriendly community that I packed in and came here!
I have learned to avoid StackOverflow - I did post there a bit at one time but it is such a poisonous, unfriendly community that I packed in and came here!
Well, it is true, but it is also have a great knowledge base. Many threads here ends with stackoverflow link to the solution or explanation.
Could you explain to me why you need the <dataType> for the return value but not the argument?
Because compiler parses code left-to-right and when you get to the argument list (but not before) it knows that it is a tCArray<dataType> and all references to tCArray (without template parameters) are to threat as current class type. You can do this if you do not want to write template arguments:
1 2
template<typename dataType>
auto tCArray<dataType>::operator=(const tCArray &cArray) -> const tCArray&
VERY cool!!! Would you believe I just read about that 'auto'/'->' operator thing in the 'new to c++11' list the other day! It didn't make much sense to me at the time but now I think I can see what it means!
I suppose in regards stackoverflow one can always just hit them up for information and not join in with all the politics and confrontational bullshit!
You are not a fan of Herb Schildt then? I must admit I quite like his books - but I will certainly check out that title you suggest as well.
I see - so by using 'auto' and '->' you are actually making the function even MORE general - by not setting out its return type until AFTER it has been given its arguments?
Does this tie in with 'Runtime Type Information'? That is another language feature I have never really understood.
Also, instead of Schildt is there an author you yourself would recommend? I'll say in advance I don't like that 'Ivor Horton' bloke. There was always something about how he wrote that got my back up!!!
Nope. Everything related to templated parts is calculated in compile time. RTTI is a mostly transparent part of the language which you probably will not directly encounter for quite some time.
Also, instead of Schildt is there an author you yourself would recommend?
Depending on which kind of books you want. Good beginner level are C++ Primer (Do not mix up with C++ Primer Plus which is bad) by Stanley Lippman and Barbara Moo. Accelerated C++ by Andrew Koenig and Barbara Moo
I posted link before to the large list of good C++ books. AFAIR all books I recommended are there too.