Jump to content

Create new public variable from method.


Kip
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

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

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 by Kip
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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 by ascendant
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...