C++ Learning Community Forum
August 01, 2010, 02:46:33 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: global class  (Read 293 times)
nofx
Jr. Nerd
***
Posts: 41


View Profile
« on: February 20, 2010, 11:41:48 PM »

Hey, how can i define an instance of a class global?
Like in this example:

Code:
class cBlaat
{
public:
   DoSomething();
};

cBlaat * blaat;

int main()
{
blaat->DoSomething();
return 0;
}

This won't work because i'm trying to define the class cBlaat globally.
Is there anyway to do this??
Logged
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #1 on: February 21, 2010, 12:19:27 AM »

Code:
#include <iostream>

class cBlaat
{
public:
    void DoSomething()
    {
        std::cout << "Hello\n";
    }
};

cBlaat blaat;

int main()
{
    blaat.DoSomething();
    return 0;
}
Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
nofx
Jr. Nerd
***
Posts: 41


View Profile
« Reply #2 on: February 21, 2010, 12:38:53 AM »

Thanks, that did the trick. But why doesn't it work when i declare it with a pointer cBlaat* blaat ..?? How come that only works when i do it inside of a function??
And what are the differences? Except that with one i use '->' to access the methods and with the other i simply use a dot '.'
Logged
charlie
Is the meadow on fire?
Dr. of C++ology
****
Posts: 730


CsGYh8AacgY


View Profile
« Reply #3 on: February 23, 2010, 07:53:29 PM »

When you add the * to the declaration, you're creating a pointer. In order to use that pointer, you have to make it point at something. The code you had shouldn't work inside a function either, because you never set blaat to point to any memory.

The fixed version from KTC just creates an object. You can use that object because it has been fully created.

In general, it's better to use the object like KTC does. There are specific instances where a pointer is a good choice, but you should wait until you learn about those before trying to use them.

Also, you should probably put your variable back inside a function. There are specific instances where a global variable might be a good choice, but in general it is better to keep your variables local to the functions you use them in.
Logged
mailsubhra
Jr. Nerd
***
Posts: 64


View Profile
« Reply #4 on: February 24, 2010, 07:29:56 AM »

Code:
#include <iostream>

class cBlaat
{
public:
    void DoSomething()
    {
        std::cout << "Hello\n";
    }
};

cBlaat * blaat;

int main()
{
    blaat -> DoSomething();
    return 0;
}

Output:
Quote
[subchatt@indl120175 C++]$ c++ testPointer1.cpp
[subchatt@indl120175 C++]$ ./a.out
Hello
[subchatt@indl120175 C++]$

On the contrary:
Code:
#include <iostream>

class cBlaat
{
public:
    int i;
    void DoSomething()
    {
        std::cout << "Hello\n";
    }
    void setVal(int arg_i){
        i = arg_i;
    }
    int getVal(){
       return i;
    }
};

cBlaat * blaat = new cBlaat();

int main()
{
    blaat -> DoSomething();
    blaat -> setVal(3);
    std :: cout << blaat -> getVal() << std :: endl;
    return 0;
}
Output:
Quote
[subchatt@indl120175 C++]$ c++ testPointer.cpp
[subchatt@indl120175 C++]$ ./a.out
Hello
3
[subchatt@indl120175 C++]$

Changing
Code:
cBlaat * blaat = new cBlaat();
to
Code:
cBlaat * blaat;
will give a segmentation fault because no memory has been allocated.
Logged
mailsubhra
Jr. Nerd
***
Posts: 64


View Profile
« Reply #5 on: February 24, 2010, 07:34:27 AM »

Please do not read my previous post. It is wrong.
Reply from charlie is absolutely correct. Ideally there must be a memory allocated in order a member function or a member variable to be accessed.
Code:
#include <iostream>

class cBlaat
{
public:
    void DoSomething()
    {
        std::cout << "Hello\n";
    }
};

cBlaat * blaat;

int main()
{
    blaat -> DoSomething();
    reinterpret_cast<cBlaat*>(NULL) -> DoSomething();
    return 0;
}

Output:
Quote
[subchatt@indl120175 C++]$ c++ testPointer1.cpp
[subchatt@indl120175 C++]$ ./a.out
Hello
Hello
[subchatt@indl120175 C++]$
Logged
mailsubhra
Jr. Nerd
***
Posts: 64


View Profile
« Reply #6 on: February 24, 2010, 07:41:11 AM »

Quote
The code you had shouldn't work inside a function either, because you never set blaat to point to any memory.
Correct said charlie. Just because you compiler is not reporting an error does not mean that it is the correct usage. It is the sheer power of you compiler that is not making the condition fail. (Say thanks to your compiler and its designers)
Ideally
Code:
reinterpret_cast<cBlaat*>(NULL) -> DoSomething();
should have failed reporting segmentation fault. But it isn't. Rather it is calling and executing the correct function. But it doesn't mean that calling a member function using NULL is the right/appropriate way of coding.
Such cases should always be avoided since they are totally compiler dependent and one never knows how and when they might fail.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!