Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7624513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:59:02+00:00 2026-05-31T04:59:02+00:00

In this code the constructor is called twice. How do I avoid that? If

  • 0

In this code the constructor is called twice.

How do I avoid that?

If I uncomment the default constructor code block then code does not give satisfactory output..

And I also want conditional based instantiation of template so I used void pointer.

#include<iostream.h>
template<class Type>
class Data{
      public:
      Type val;
      Data(Type v){
                cout<<"In Constructor Param";
                val = v;
      }
      Data(){
             //  cout<<"In Constructor Defa";  uncommnet this line
      }
      ~Data(){}
};
int main(){
    Data<void *> obj;
    obj = new Data<float>(31.34f);
    cout<<*(float*)obj.val;
}

Output:

In Constructor Param
In Constructor Param
31.34

Thanks for involving.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:59:03+00:00Added an answer on May 31, 2026 at 4:59 am

    Because you are creating three objects. Your code contains an implicit conversion from Data<float>* to Data<void*>, via the conversion constructor Data<void*>::Data(void*), and is equivalent to

    Data<void *> obj;                              // first object
    Data<float> * temp = new Data<float>(31.34f);  // second object
    obj = Data<void *>((void*)temp);               // third (temporary) object
    

    I’ve no idea how to avoid that, because I’ve no idea what your code is trying to do. You could prevent weird conversions like that by declaring the constructor explicit, so that it doesn’t allow implicit conversions.

    Also, whichever book you’re using to learn C++ is very outdated. Since 1998 (and possibly earlier), the standard I/O header has been called <iostream> with no .h, and all the standard library’s names like cout have been in namespace std.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have an object called FixedSizeList that does not have a parameterless constructor.
In this code fragment, which constructor is actually called? Vector v = getVector(); Vector
I wrote this code. The constructor works normally, but in the destructor I get
I have non-template class with a templatized constructor. This code compiles for me. But
In the code that follows, the object sub in class C is constructed twice.
Let me start with this is not a repeat of Why does __init__ not
The code below illustrates the destruct() being called twice. I'd like to know why?
I run this code for experimenting copy constructor and assignment operator class AClass {
There is this code: #include <iostream> class Base { public: Base(){ std::cout << Constructor
I have lots of code like this in my constructors:- function __construct($params) { $this->property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.