I am learning/relearning C/C++. I have created a template class with various working functions. I have a problem when I try to use code that works for a char datatype with an AnsiString.

If I call a template class function by calling a template function and passing a variable "Data" that I have created as an AnsiString, but not set equal to any value as of the time that it is passed, I find that if I try to set "Data" equal to any string in the template class function that is called, I can assign a value to "Data" in the template class function and display it, but can not access the string I stored in "Data" in the template class function when control is returned to the code block in which I called the template class function.

This is not a problem if I use a similar call to a template function in the class I have defined using a char. In fact, if I call the template function and pass Data as an AnsiString, I can assign a value to "Data.strcpy()" and without returning that value access "Data.strcpy()" in the block in which I call the template class function (of course, only after the function is called).

I need to be able to change the value of an AnsiString variable "Data" in a template class function, then access the same AnsiString variable "Data" in the calling code block immediately after the function call. I do not want to use the "Data.strcpy()" method to access it after I call the function by setting "Data.strcpy()" equal to the string I want returned in the template class function, then setting an AnsiString "Data" equal to "Data.strcpy()", which will work, but is awkward and introduces an inconsistency.

In part, I don't understand why I can change the string stored in a char variable in a template class function after calling it, then access the char, but can not do the same thing with an AnsiString variable.

I did read that the 4 byte long AnsiString variable maintains a counter that changes when the variable value is changed, and that the same address can be associated with two different AnsiStrings. I believe that the compiler is treating the "Data" variable references, when they are defined as an AnsiString, as two different variables, one associated with the calling code block, and the other with the template class function. The char references are pointers, and do not change when sent to the template class.

I would like to know how to send an AnsiString variable called "Data" to a template class function so that the calling block can immediately access the value associated with the AnsiString variable "Data" that is assigned to it in the template function after control is returned to the line in the calling block of code after the function is called. I believe I need to find a way to make the "Data" AnsiString variable in the calling block the same one that is sent to and into which a string is loaded by the template class function. This should enable me to access the string stored in "Data" by the template class function in the calling block.

Thank you in advance for any help that you can provide into dealing with this problem. I'm sure that it is trivial for an experienced C++ programmer.

I would like to know how to send an AnsiString variable called "Data" to a template class function so that the calling block can immediately access the value associated with the AnsiString variable "Data" that is assigned to it in the template function after control is returned to the line in the calling block of code after the function is called. I believe I need to find a way to make the "Data" AnsiString variable in the calling block the same one that is sent to and into which a string is loaded by the template class function. This should enable me to access the string stored in "Data" by the template class function in the calling block.

That's a long winded way of saying you want to pass a reference. Why don't you give some pseudocode that shows us exactly what you want instead of trying to describe it in an ambiguous way?

That's a long winded way of saying you want to pass a reference. Why don't you give some pseudocode that shows us exactly what you want instead of trying to describe it in an ambiguous way?

----------------------
The requested pseudocode would read something like:

From main routine that calls template class function:
...
AnsiString mycat;
mycat = "Persian";
ChangeCatType(mycat);
RichEdit1->Text = mycat;
...

From template class function:

...
ChangeCatType(Type)
{
mycat = "Siamese";
}


Output in RichEdit1 control would then read: Siamese

Note: I can do this by passing a char, so that isn't the answer I am seeking. I want to find out if I can do this by passing an AnsiString variable to the template class function and thereby change the content of the AnsiString printed in the RichEdit1 control through the action of the template class function alone without having to add c_str() to the AnsiString variable in the main code segment or altering the type of variable sent to or received from the template class function. I am willing to manipulate the AnsiString and its representation within the template class function, as long as I don't have to do anything to the variable name in the main routine and can immediately print the AnsiString in a RichEdit control as indicated in the "pseudocode" without having to alter the variable name as part of the reference to the string value to print in the main code block (after the calling code).

It is important to recognize that changing the type of cat that is contained in a string variable is not the purpose of this inquiry. That can clearly be done quite directly without calling any functions. The purpose of this inquiry is to learn if one can send an AnsiString variable to a template class function, change the string it contains within the template class function by whatever means, but by means restricted to the interior of the template class function, then directly access the changed information in that AnsiString variable in the MAIN code block immediately after calling the template class function that changes the contents of the AnsiString variable.

Thanks to anyone who can clear this up and establish if what I have proposed can be done and how in a generalized fashion.

Unless you're not describing your problem adequately, you want a reference:

template <typename T>
void ChangeCatType(T& x)
{
  x = "Siamese";
}

AnsiString mycat;
mycat = "Persian";
ChangeCatType(mycat);

Unless you're not describing your problem adequately, you want a reference:

template <typename T>
void ChangeCatType(T& x)
{
  x = "Siamese";
}

AnsiString mycat;
mycat = "Persian";
ChangeCatType(mycat);

-----------------------------------
Thank you!

Is there anyone who can tell me the difference between use of "typename" and "class" in defining a template function? Is there anyway to use the variable type in an "if-then" statement byusing the variable type, including your own, homemade class type, to determine how it is processed in a function? Resorting back to pseudocode:

if (mycat is a char)
{

mycat = "cat";
}

if (mycat is an int)
{
mycat = 3 * pi * mycat;
}

Thank you most sincerely once again.

>Is there anyone who can tell me the difference between use of "typename" and "class" in defining a template function?
There is no difference. I find typename to be more intuitive, but you can use whichever you like.

if (mycat is a char)
{

mycat = "cat";
}

if (mycat is an int)
{
mycat = 3 * pi * mycat;
}

You can solve this problem with either polymorphism or run-time type identification. The following is one way for the latter:

#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

int main()
{
  int mycat;

  if (string(typeid(mycat).name()) == "char")
  {
    cout<<"char"<<endl;
  }
  if (string(typeid(mycat).name()) == "int")
  {
    cout<<"int"<<endl;
  }
}

I'm sure that you can figure out how to do it with polymorphism.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.