2
struct student{
    char name[20];
    in roll;
};

int main()
{
    struct student s1;
}

Can we restrict the structure instance to only one. i.e. if we write struct student s1,s2; ,then it should give error. Which means struct student s1; should be allowed. Can we do so in c or c++;

Benjamin Bannier
  • 45,636
  • 10
  • 55
  • 78
fresher
  • 280
  • 2
  • 12

3 Answers3

5

Can we restrict the structure instance to only one?

Of course you can: by not creating another instance.


Restricting the number of instances of a class is often not what you want. And no, you shouldn't use the Singleton pattern at all.

Community
  • 1
  • 1
Shoe
  • 70,092
  • 30
  • 150
  • 251
1

The answer is depending on the language (you tagged both Cand C++).

In C it is not possible if the structure definition is known. You may be able to implement an encapsulated module where only the pointer to a struct is exposed, but such an implementation might be rather limited. For a plugin-system this might be usefull though.

In C++ you can do this implicitly via a Singleton pattern. However this wouldn't prevent you from having multiple variables, as shown in your example, it only ensures that all these variables are using the same instance. It would also give no error, at least no compile error. You could throw a runtime error though.

Devolus
  • 20,356
  • 11
  • 56
  • 104
  • You can make the constructor `private`, with the singleton-producing function a `friend`. Not claiming this is a good idea though. – BoBTFish Dec 09 '13 at 13:10
0

Yes. You can create your structure as a singleton in a class-like style. Consider example below:

struct Singleton
{
private:
    static Singleton * instance;

    Singleton()
    {
    }

    Singleton(const Singleton & source)
    {
        // Disabling copy-ctor
    }

    Singleton(Singleton && source)
    {
        // Disabling move-ctor
    }

public:
    Singleton * GetInstance()
    {
        if (instance == nullptr)
            instance = new Singleton();

        return instance;
    }
}

Of course if you don't want to return a pointer to existing instance when created a second time, you can implement there some exception throwing / other error message.

Zegar
  • 995
  • 9
  • 17
  • 5
    `Singleton& GetInstance() { static Singleton instance; return instance; }` is the idiomatic and correct way to write a singleton. – Simple Dec 09 '13 at 12:58
  • @Simple I've seen dozens of singletons, I've never heard of this "correct way", can you point me to some literature? – NiRR Dec 09 '13 at 13:00
  • if using C++11-supporting compiler, `= delete;` is nicer than declaring ctors&co private – TeaOverflow Dec 09 '13 at 13:00
  • If you use C++11 just `=delete` functions you want to make unavailable (or at least leave them unimplemented so they cannot be called). – Benjamin Bannier Dec 09 '13 at 13:01
  • @NiRR: This is *Meyer's singleton* and it is even thread-safe in C++11. – Benjamin Bannier Dec 09 '13 at 13:03
  • @NiRR it's called the Meyers Singleton. It's in his first book Effective C++, but you can learn about it online. – Simple Dec 09 '13 at 13:03
  • @Simple: There's no "correct way", although your suggestion does have the fewest deathtraps. Just be careful accessing it from the destructor of a static object - it might already have been destroyed. (The leaky version in this answer avoids that deathtrap, but isn't thread-safe). – Mike Seymour Dec 09 '13 at 14:34