Home > Back-end >  C why template class can reference character array? In the face of the array in C direct reference i
C why template class can reference character array? In the face of the array in C direct reference i

Time:11-09

//write a template class yesterday, found that is defined as the character array after it is lawful to reference, feel very strange, then write a general reference found that it was illegal, code is simplified as follows:

TemplateThe class test
{
Public:
The test (const T & amp; A)
{
}
};

The class test1
{
Public:
Test1 (const char * & amp; A)
{}
};



Int main ()

{

TestTest1 * t1=new test1 (" fun ");//illegal
return 0;

}

//don't understand why, just contact template class, meet you

CodePudding user response:

I think I may be because the const didn't modify & amp; The reason, if you define an alias:
Typedef char * PChar;
And then the test1 (const PChar & amp; A);
I don't know this line not line?

CodePudding user response:

If test

CodePudding user response:

refer to the second floor early play big play nuclear response:
if test
I think this was the reason for the illegal t in front of the pointer is a pointer to receive very variable type, and new back out is a pointer to receive often variable class, the compiler that this is not a pointer to the wrong?

CodePudding user response:

reference 1st floor SuperDay response:
I think I may be because the const didn't modify & amp; The reason, if you define an alias:
Typedef char * PChar;
And then the test1 (const PChar & amp; A);
I don't know this line not line?
yes, it is ok, but not about whether const modifier to & amp; Problem, I again write a class
The class test2
{
Public:
Test2 (char * & amp; A)
{}
};
Test2 * t2=new test2 (const_cast & lt; Char * & gt; (" fun "));
What is still an error ah, that is the reason? And I remember in c + + array direct reference is illegal

CodePudding user response:

Look not const modifiers cannot use string literals, as a reference type, the test (const T & amp; A) remove the const here is also pass the
  • Related