Sign in to follow this  
Followers 0
Kip

Create new public variable from method.

36 posts in this topic

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.

Share this post


Link to post
Share on other sites



Hmm. Well, is there a way to change the datatype of Hellow? Because that was all I was trying to do.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

something like this: (and this is just a very short example)

typedef int MyType;

MyType var = 9;

typedef double MyType;

var = 9.896;

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by Kip

Share this post


Link to post
Share on other sites

Does a union do what you want? You're being very vague and contradictory so it's hard to suggest anything.

Share this post


Link to post
Share on other sites

... so you want a variant.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

No, I want a function (or whatever) to change the datatype. If I wanted a variant I would have used Boost.

Edited by Kip

Share this post


Link to post
Share on other sites

Wow you're dumb. Have fun.

Share this post


Link to post
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)

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

#19 ·  Posted (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 by ascendant

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0