I want to be able to write a class that does not require a name in order to use it. For example, cin is a class but it does not require you to create a variable and then access it through that variable.
For example cin is not used like this:
int number;
class_name variable_name;
variable_name.cin >> number;
How do I do this?
classes without named instances
Started by
Guest_Coder87_*
, Apr 04 2007 03:25 PM
2 replies to this topic
#1
Guest_Coder87_*
Posted 04 April 2007 - 03:25 PM
Guest_Coder87_*
|
|
|
#2
Posted 05 April 2007 - 08:09 AM
cin is a named instance of a class. To not use a named instance, you will have to use a named pointer to the class and use the new keyword to create the object pointed to.
#3
Posted 09 April 2007 - 09:22 AM
If you want only a single object of a class, you can do like this;
It's tested with GCC.
class
{
public:
void foo()
{
std::cout << "Hello, World!" << std::endl;
}
} myObject;
and then use it like this...myObject.foo();I don't think it's a good programming practice, but it's possible.
It's tested with GCC.


Sign In
Create Account

Back to top










