Kip Posted March 22, 2009 Share Posted March 22, 2009 Hi, I'm trying to create a new public variable in a function. Example: class MyClass { public: int hellow; MyClass() { long this->newvar; // doesnt work. (ofcourse), But I hope you'll get the idea. // I want it to be created in the same scope as 'hellow' } }; Somebody knows how to do it? Thanks. MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
jvanegmond Posted March 22, 2009 Share Posted March 22, 2009 You can't do that. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Kip Posted March 22, 2009 Author Share Posted March 22, 2009 Hmm. Well, is there a way to change the datatype of Hellow? Because that was all I was trying to do. MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
ProgAndy Posted March 22, 2009 Share Posted March 22, 2009 why do you have to change a datatype? I think that's not possible. *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes Link to comment Share on other sites More sharing options...
James Posted March 22, 2009 Share Posted March 22, 2009 I assumed he wants something like TypeDef but then he can't have two things with the same name (Kip ) Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Valik Posted March 22, 2009 Share Posted March 22, 2009 Kip, this is a case where you need to describe what problem you are trying to solve rather than try some implementation that doesn't make sense for the language. C++ is a heavily typed language. What you are asking to do makes no sense in C++. Without an adequate description of what exactly you hope to achieve, there's no way to offer a C++ solution to the problem. Link to comment Share on other sites More sharing options...
Kip Posted March 22, 2009 Author Share Posted March 22, 2009 something like this: (and this is just a very short example) typedef int MyType; MyType var = 9; typedef double MyType; var = 9.896; MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted March 22, 2009 Share Posted March 22, 2009 It's still not clear if you want a variant or if you can get by with using a union. I'm thinking you want a variant, however. Link to comment Share on other sites More sharing options...
Kip Posted March 22, 2009 Author Share Posted March 22, 2009 (edited) Not really a variant, I don't want it to be able to hold all kinds of datatypes. I just want it to hold one datatype, and let the type change. You can say it's like a variant, yes. this is a variant: variant Hi = "you"; Hi = 9; Hi = 9.827434; and this is what I want: int Hi = 8; switch_type<string>(Hi); //or whatever is needed to change the datatype. Hi = "you"; switch_type<double>(Hi); Hi = 9.28372; Edited March 22, 2009 by Kip MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted March 22, 2009 Share Posted March 22, 2009 Does a union do what you want? You're being very vague and contradictory so it's hard to suggest anything. Link to comment Share on other sites More sharing options...
Kip Posted March 22, 2009 Author Share Posted March 22, 2009 No, I just updated my previous post, please take a look at the examples. MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted March 22, 2009 Share Posted March 22, 2009 ... so you want a variant. Link to comment Share on other sites More sharing options...
Kip Posted March 22, 2009 Author Share Posted March 22, 2009 (edited) No, I want a function (or whatever) to change the datatype. If I wanted a variant I would have used Boost. Edited March 22, 2009 by Kip MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted March 22, 2009 Share Posted March 22, 2009 Wow you're dumb. Have fun. Link to comment Share on other sites More sharing options...
Ascend4nt Posted March 22, 2009 Share Posted March 22, 2009 Kip, I think you really should use a Union. And then just access the type you want set. MyVar.AsCharPtr="String", MyVar.AsInt=14. Of course, it can only store one value at a time - and you need to make sure the Union contains the largest type of object you wish to hold, plus a mechanism for determining which type is the 'current' type (where a structure/class would come in) My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
cppman Posted March 22, 2009 Share Posted March 22, 2009 Not really a variant, I don't want it to be able to hold all kinds of datatypes. I just want it to hold one datatype, and let the type change. You can say it's like a variant, yes. this is a variant: variant Hi = "you"; Hi = 9; Hi = 9.827434; and this is what I want: int Hi = 8; switch_type<string>(Hi); //or whatever is needed to change the datatype. Hi = "you"; switch_type<double>(Hi); Hi = 9.28372;You can't. You could, however, if C++ was an interpreted language (and it supported such a strange feature). Since C++ is not interpreted, it has to know what data-type a variable is in order to generate the proper assembly. Why do you not want to use a variant? It would have the same effect except the "switch_type<blah>()" would be implied. Miva OS Project Link to comment Share on other sites More sharing options...
Kip Posted March 23, 2009 Author Share Posted March 23, 2009 Why do you not want to use a variant? It would have the same effect except the "switch_type<blah>()" would be implied.Yeah, I'm using it now. MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
JSThePatriot Posted March 23, 2009 Share Posted March 23, 2009 Not fun to program with, but it is available static_cast<int>(MyType) static_cast<double>(MyType), or alternately any of the *_cast<>() functions. Again, I wouldn't recommend this as it's still not plain what you're trying to really accomplish, and if you have it solved anyways no biggie. Was trying to provide some further assistance. Jarvis AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
Ascend4nt Posted March 24, 2009 Share Posted March 24, 2009 (edited) Can someone point me to a good source or recommend a book that describes 'Variants'? There is no mention of it in any of the books I have on C++ (though somewhat dated), but even the C++ standard docs describe only unions as having variant data, and there's some references to it regarding the Component Object Model, but only simple descriptions.'VARIANT' doesn't look like a native data type, and I'm just wondering where to go to get a good description on it.Thanks.*edit: argh, nm - forgot to search MSDN. Here's the 'VARIANT and VARIANTARG' link. Edited March 24, 2009 by ascendant My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
Richard Robertson Posted March 24, 2009 Share Posted March 24, 2009 Variant is a composite type made of multiple native types or composite types contained together OR an untyped pointer to a native type or composite type. Apart from this field is an optional field for storing the original type of the data and a number of functions for converting between types. I've got a working .Net variant. It's tedious to write in a strongly typed language. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now