Jump to content

How to? AU3 --> C++


slaultu
 Share

Recommended Posts

11 minutes ago, TheDcoder said:

Best of luck with your learning @markyrocks, it is a fun journey assuming that you enjoy what you are doing.

I do enjoy it.   Much rather do this than watch tv lol.  Maybe you can clear up something I was having an issue with. 

If I create a class in c++.  Then declare a static variable like so

Class class
    {
    Public:
    Static std::string str;
    };
    //seems like anyway I initialize it gives me a linker error?
    class c;  //is fine
     
    c.str="string";  //error
    c::str="string"; //error

I've seen this done in tutorials and it seemed to work either way but I'm using vs 2019 and the tutorial is using some form of vs. Doesn't seem to error in the videos.... any thoughts?

The only reason I'm interested in this is bc some member variables don't need to have a separate instance for every object.   Like is str was the date.  It doesn't matter what instance of the class I'm using the date should be the same.

Edited by markyrocks
Link to comment
Share on other sites

5 minutes ago, markyrocks said:

Maybe you can clear up something I was having an issue with. 

Sorry, I do not program in C++ so I don't know the specifics of how objects/classes work there. Someone more capable will answer your question :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@markyrocks You're issue is 

Quote

Static members of a class are not associated with the objects of the class: they are independent variables

 so you must define them like:

class Myclass
    {
    public:
    static std::string str;
};
std::string Myclass::str="Hello World";


int main()
{
    std::cout << Myclass::str << "\n";
    Myclass::str="Hola Mundo";
    Myclass obj;
    std::cout << obj.str << "\n";
    return 0;
}

Saludos

Link to comment
Share on other sites

57 minutes ago, Danyfirex said:

@markyrocks You're issue is 

 so you must define them like:

class Myclass
    {
    public:
    static std::string str;
};
std::string Myclass::str="Hello World";


int main()
{
    std::cout << Myclass::str << "\n";
    Myclass::str="Hola Mundo";
    Myclass obj;
    std::cout << obj.str << "\n";
    return 0;
}

Saludos

Ty yes I was playing around with this a bit last night and came to the conclusion that I was declaring it out of scope?  Declaring the static variable inside of the main must make it invisible to the compiler.   I'm assuming declaring it outside of the main makes the variable global.   At least that's my take on it.  Tutorials often don't make fine details like this explicitly clear, well at least the ones I'm watching anyways.

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...