#include<iostream.h>
#include<conio.h>
typedef class
{
     int i;
     float b;
     public:
     obj()
     {
	cout<<"\n Is this constructor \n";

     }
     void show()
     {
	cout<<"fkjhfg";
     }
}obj;

void main()
{
     clrscr();
     obj o;
     o.show();
     getch();
}

In this code ,of Anonymous class object is calling the show function ,but its constructor is not called. Can some body Explain me this behaviour of the anonymous class.

Dani AI

Generated

Short answer: the function you wrote inside that anonymous typedef is not a constructor, so nothing prints when the object is created. As and said, a user-defined constructor must use the class's actual name (the class tag). A typedef-name introduced after an anonymous class is not the class name inside the class body, so you never defined a constructor — the compiler quietly supplies an implicit default constructor that does nothing.

Why this happens (brief, precise): a C++ constructor is a special member function whose name must match the class name; the language provides an injected-class-name for that purpose when the class has a tag. An unnamed (anonymous) class has no injected-class-name, and the trailing typedef-name is introduced only after the class-specifier completes. See the language reference on Constructors and the note about injected-class-name for the formal rules. The compiler will generate an implicit default constructor when you have not provided one — read about the default constructor.

Practical advice: give the class a proper name (avoid the C idiom of typedef-ing anonymous classes in C++), or use aggregate/brace initialization if you want trivial initialization. Use a modern, standards-conforming compiler with warnings enabled (for example, enable -Wall -Wextra and a recent -std=c++17 or newer) so the compiler will diagnose attempts to declare a constructor incorrectly. Also avoid nonstandard bits like <iostream.h>, conio.h and void main in new code.

Recommended Answers

All 2 Replies

>but its constructor is not called
The constructor is called, you just didn't define it. This:

obj()
{
  cout<<"\n Is this constructor \n";
}

is not a constructor. It's an ill-formed member function that happens to have the same name as your typedef. If your compiler doesn't warn about this (as well as void main and pre-standard headers), you need a newer compiler.

The problem is that you're confused about the difference between a class name and a typedef tag. A typedef tag does not act in place of a class name, but since C++ doesn't require an explicit class keyword when instantiating objects, I can't imagine why you'd even want to do this. The following code is much much better:

#include <iostream>

class obj {
public:
  obj()
  {
    std::cout<<"This is a constructor\n";
  }

  void show()
  {
    std::cout<<"fkjhfg\n";
  }
};

int main()
{
  obj o;
  o.show();
  std::cin.get();
}

In C++ a struct or class needs to have a name to have a user defined constructor. The compiler will also generate the usual compiler generated constructors, but you can't get at them normally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.