Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 04-14-2006, 02:02 PM   #1
Baseband Member
 
Dnsgm's Avatar
 
Join Date: Jan 2006
Posts: 61
Default declaring a CLASS embedded in a VOID

I have a class in a void, but I'm having trouble declaring it from main().

Code:
#include <iostream>
using namespace std;

void asdf ()
{
     class member
     {
           public:
                  member ()
                  {
                         cout<< "Hello! \n";
                  }
                  
                  ~member ()
                  {
                          cout<< "Goodbye! \n";
                  }
     };
     
}

int main ()
{
    
    // How do I declare the Class? */
 
    return 0;
}
__________________

Dnsgm is offline   Reply With Quote
Old 04-15-2006, 01:53 PM   #2
Baseband Member
 
Griz803's Avatar
 
Join Date: Jun 2004
Posts: 89
Default Re: declaring a CLASS embedded in a VOID

What your code shows is a class that you are declaring inside a function that returns void. It can't legally happen and not get instantiated, used and destroyed inside that function. In other words, the specifications say, you've limited the scope of the class to the scope and lifetime of the function you put it in. So, unless you use it after the function call is made and before the function returns, you lose it

Try this again without the void blah() function and braces. The class declaration(actually, definition) itself is correct, it just can't stay and play long because the scope is lost before any useful stuff happens. I hope this helps you out.

Code:
#include <iostream>
#include <cstdlib>  // May need this for the pause statement to work.
using namespace std;

//void asdf () This isn't needed here and creates a problem.
// {  Ditto
     class member  // This begins the declaration AND DEFINITION of class           {
           public:
                  member ()
                  {
                         cout<< "Hello! \n";
                  }
                  
                  ~member ()
                  {
                          cout<< "Goodbye! \n";
                  }
     };//End of definition of class member.
     
// }  Mustn't forget this!  It'll mess it up.

int main ()
{
    
    /*How do I declare the Class? Roughly, 
       Like this below.  However, you are actually creating 
       an instance or instantiating the class.  It was declared and 
       defined together above.*/

    member Mine;
    /* Note that the virtual constructor is called here. 
        Next line inserted in case you need to see what is happening
        which usually happens working in Dev-C++ */
    system("pause");
    return 0;
// Destructor is called here as program goes out of scope and makes auto call.
}
I hope I've explained it better and helped you out
__________________
/*What does not kill us, makes us drink far too much caffeine*/
Griz803 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:51 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0