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 6084377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:30:52+00:00 2026-05-23T11:30:52+00:00

For standard data objects such like int, the following can be done int number;

  • 0

For standard data objects such like int, the following can be done

    int number;
    number = 0;

Basically, you can declare number before initializing it, useful if you initialize inside various if statements and you don’t want number going out of scope.

Can something similar be done with custom classes?

I have a class called mem_array with constructor of the form

    mem_array(int,int,std::string);

I would like to do the following

      mem_array myData;
      if(x==0) myData(1,1,"up");
      if(x==1) myData(0,0,"down");

basically, so I can use myData outside of the scope of the if statements. Can something like this be done?

  • 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-23T11:30:53+00:00Added an answer on May 23, 2026 at 11:30 am

    Your first line will give you an error since the constructor doesnt have default values and a constructor without parameters doesnt exist.

    Just use a pointer (or even better a smart pointer, so you dont have to take care of deleting the object). But be sure to check afterwards that x was either 0 or 1, i.e. check that myData has been constructed.

    mem_array* myData=0;
    if(x==0) myData=new mem_array(1,1,"up");
    if(x==1) myData=new mem_array(0,0,"down);
    
    assert(myData!=0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a container class for adding some properties to standard data types like
I have a grid that binds a number of child data objects to columns
The standard data source configuration in JBoss requires the username and password of the
What does the castle validators offer me over standard data annotations? I am a
With a pretty standard Dynamic Data site, when the user edits or inserts a
I am building a small website for fun/learning using a fairly standard Web/Service/Data Access
The standard method to send data on a stream socket has always been to
In our data access layer at work we have this standard implementation where the
Generally I connect and retrieve data using the standard way (error checking removed for
I'm writing a simple data UI using standard .Net databinding to a typed DataSet

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.