• 0

Creating a Object from a class automatically.


Question

#include <iostream>
using namespace std;

class test {
  public:
     test(int one, int two);      //concustructor taking two arguments.

  private:
}; one (1, 1)
//one will be created as an ojbect -- but as now the compiler will give me an error

void main()
{

}

Is there a way when this class has been defined that I can create a object with it so the user does not have to?

By that I mean the user will use the class methods, but the object deprived from the class test will have been already created.

So in main I do not have to declare say test one. Sorry I can't be more clearer on this but it is the same way you can do it with structures.

Thanks.

8 answers to this question

Recommended Posts

  • 0

cant test my c++ code, but I think this would work...???

class Test

{

public:

static Test singleton; //static variable to trace instance of class

Test(); //concustructor

Test(int one, int two) //another constructor

public getInstance();

public getInstance(int one, int two); //why would you do this if exists?

public doSomething();

public Test getInstance()

{

if (self.singleton == null)

self.singleton = new Test();

return self.singleton;

}

public Test getInstance(int one, int two)

{

if (self.singleton == null)

self.singleton = new Test(one, two);

return self.singleton;

}

public main

{

Test.getInstance(1, 2).doSomething();

Test.getInstance().doSomething();

}

};

  • 0

Wow -- that looked a lot like Java :), but alas that didn't work. It seems it is on the right track though. I am sure that has to be a way. I basically want to build a class with a pre-defined object from that class that will be used by another class. Sort of like aggregation, but not inheritance. So while others can create different objects from the class -- they won't this time around and will just use the pre-defined object.

Self learning C++ is a lot hard than I thought it would be.

  • 0

Not really a lot of Java at all took 2 minutes...

the key thing is to keep a reference to one instance of your class .. thus the variable

static [insert class name here] singleton;

I'm pretty sure the C++ code I gave above would be close to working, just need to adjust syntax slightly.

another thing people do is construct/init the object when declaring global variable

class X

{

X singleton = new X();

}

Edited by liykh001
  • 0

it's not C++ code. You don't decorate implementation with public. And what is self? It's this. You can't use this with static members. Here's a C++ example of a singleton.

#include <iostream>

class Test
{
public:
	static Test& getInstance();
	void doSomething();
private:
	static Test singleton; // variable to trace instance of class
	Test(){ std::cout << "Inside private constructor" << std::endl; } //concustructor
};

Test Test::singleton;

Test& Test::getInstance()
{
	std::cout << "In getInstance()" << std::endl;
	return singleton;
}

void Test::doSomething()
{
	std::cout << "Did something" << std::endl;
}

int main()
{
	Test t = Test::getInstance();
	t.doSomething();
	return 0;
}

  • 0
it's not C++ code. You don't decorate implementation with public. And what is self? It's this. You can't use this with static members. Here's a C++ example of a singleton.

damm my memory, Thanks

gee it has been a long time since I touched C .... hmm I think it has been about 5 years.

  • 0

int main()
{
 ? ?Test t = Test::getInstance();
 ? ?t.doSomething();
 ? ?return 0;
}

I have a question, in C++

int main()
{
 ? ?//java you can do this
 ? ?Test.getInstance().doSomething();
 ? ?
 ? ?//C++ can you do this?
 ? ?Test::getInstance()::doSomething();

   //or 
   Test::getInstance().doSomething();
 ? ?
 ? ?return 0;
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Segra 1.6.0 by Razvan Serea Segra is a free, open-source OBS-powered game recorder offering fast gameplay capture, instant clips, AI highlights, deep game integration, and seamless uploads—perfect for gamers, streamers, and content creators. Lightweight, fast, zero bloat. Segra key features: Automatic Game Recording: Begin capturing gameplay the moment your game launches, with zero manual setup. Instant Clipping: Save important moments instantly using a customizable hotkey—perfect for highlights, montages, or quick shares. Segra AI Highlights: Let Segra automatically detect kills, assists, deaths, and key events to generate polished highlight reels without manual editing. Gameplay Uploads: Upload recordings and clips directly to Segra.tv for fast sharing and cloud access. Deep Game Integration: Enjoy advanced game-data tracking across hundreds of supported titles, enabling smart highlight generation and stat-informed clipping. High-Performance Capture: Record up to 4K at 144 FPS using OBS-powered technology with minimal performance impact, supporting NVENC, AMD VCE, and custom quality controls. Segra Editor: Edit recordings easily with timeline controls, segment management, and event-based navigation to build the perfect clip. Customization Options: Adjust hotkeys, output formats, storage paths, codecs, capture quality, and performance settings for a tailored recording experience. Segra 1.6.0 changelog: Recording: Added HDR support. Grand Theft Auto: Added game integration for deaths (FiveM and RAGE MP supported). Highlights: Added customizable padding for highlights. Replay Buffer: Added a shockwave visual effect when a replay buffer clip is saved. Audio: Increased the maximum sound effects volume from 100% to 200%. Hotkeys: Fixed hotkeys not triggering while unrelated keys were held. Installer: Added code signing to verify publisher identity, branded the installer, and reduced OS security warnings. OBS: Updated the supported OBS version to 32.1.2. Download: Segra 1.6.0 | 74.4 MB (Open Source) View: Segra Homepage | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • According to the developer(s):    
    • Most of the sellers are selling under a business name, and you can't jail a business. The best option would be to blanket ban brick chargers and regulate fixed PD faceplates for wall as a spur from existing sockets. That way they'd be checked with during during an EICR.
    • Totally ridiculous! They wonder why people can't stand their company...
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      PsYcHoKiLLa
      176
    3. 3
      +Edouard
      163
    4. 4
      Steven P.
      86
    5. 5
      ATLien_0
      79
  • Tell a friend

    Love Neowin? Tell a friend!