• 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

    • Because of the EU (a good thing) newer android devices been getting 5 years worth of security patches. Except some Motorola which found the loop hole, and offer ZERO updates. In addition, Google for years have been making where it can patch some stuff by updating the core Google Play Store itself.  As echoed earlier,  you take the security risk in to your own hand beyond supported.
    • Win11Debloat 06.11.2026 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 06.11.2026 fixes: Fix lock screen spotlight option being disabled when disabling the start recommended section by @Raphire in #619 Fix log message formatting by @Raphire Note The -RemoveCommApps and -RemoveW11Outlook command-line parameters for uninstalling a few specific apps have been removed with this release. If you previously relied on these parameters, please see this wiki page for alternative methods of removing these apps. Download: Win11Debloat 06.11.2026 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yes for me, I installed 'old calculator' (Windows 7 calculator) in its place since it is more useful to me. I think paint is the only one I left installed
  • Popular Contributors

    1. 1
      +primortal
      511
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      154
    4. 4
      ATLien_0
      82
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!