Jump to content

classes without named instances

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Guest_Coder87_*

Guest_Coder87_*
  • Guests
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?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
If you want only a single object of a class, you can do like this;
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.