Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Per-type compile time constants?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="mario, post: 2573145"] Hi! First of all, sorry for my English, it's not my native tongue. Anyway, here we go: suppose I create a class that handles my own custom text strings. No, suppose I create TWO such classes, which differ e.g. in the characters mappings and in the implementation, etc... Necessary operators are overloaded, so that the following example works as one may intuitively expect it to work: MyFirstType MyFirstTypeInstanceA, MyFirstTypeInstanceB; MySecondType MySecondTypeInstanceA,MySecondTypeInstanceB; MyFirstTypeInstanceA=GetFirstTypeString(); MySecondTypeInstanceA=GetSecondTypeString(); MyFirstTypeInstanceB="Hello"+MyFirstTypeInstanceA+NewLine; MySecondTypeInstanceB="Hello"+MySecondTypeInstanceA+NewLine; Please focus on "NewLine". My problem is the following: I want NewLine to be a *different* constant in the two cases of above. The compiler knows, in the first case, that it is evaluating an expression of type "MyFirstType" because it is assigning the expression to an instance of that type. It also knowns, in the second case, that this time it is evaluating an expression of type MySecondType. So it has enough info to understand that NewLine, if declared as constant, belongs to its own class each time it evaluates a certain expression. But I don't know if C++ allows this, hence this post of mine. Sure, I could do something like: MyFirstTypeInstanceA="Hello"+MyFirstType::NewLine; MySecondTypeInstanceA="Hello"+MySecondType::NewLine; (I made them shorter to avoid the 78 chars Usenet limitation) But it seems awkward to me to have to specify the type before the constant, when e.g. the "Hello" conversion from char to the right type is automaticly handled in an nice way by the compiler. Is there any way in C++ to make a (preferably compile time, for performance reasons) constant depend on the "context" (I mean on the type that is being evaluated in the expression)? Sorry again for my English, it's not my native language. Thanks a lot, Mario [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Per-type compile time constants?
Top