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

  • Home
  • SEARCH
  • 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 1047981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:23:07+00:00 2026-05-16T16:23:07+00:00

I am having difficulty writing my code in the way it should be written.

  • 0

I am having difficulty writing my code in the way it should be written. This is my default constructor:

Address::Address() : m_city(NULL), m_street(NULL), m_buildingNumber(0), m_apartmentNumber(0)
{}

…and this is my other constructor:

Address::Address(const char* city, const char* street, const int buildingNumber,const int apartmentNumber) : m_city(NULL), m_street(NULL)
{
    SetAddress(city,street,buildingNumber,apartmentNumber);
}

I have to initialize my city and street fields as they contain char * and my setter uses remove to set a new city for example. I would very much like to hear your opinion on how to write it in the right way without repeating code.
this is my SetAddress code :

bool Address::SetAddress(const char* city, const char* street, const int buildingNumber, const int apartmentNumber)
{
    if (SetCity(city) == false || SetStreet(street) == false || SetBuildingNumber(buildingNumber) == false || SetApartmentNumber(apartmentNumber) == false)
        return false;
    return true;
}

and this is my SetCity:

bool Address::SetCity(const char* city)
{
    if(city == NULL)
        return false;
    delete[] m_city;
    m_city = new char[strlen(city)+1];
    strcpy(m_city, city);
    return true;
}

1 more question if i do change char* to string how can i check if string city doesnt equal to NULL as i know string does not have the “==” operator and string is an object and cannot be equal to null,
how can i check if the string i get is indeed legeal.

  • 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-16T16:23:08+00:00Added an answer on May 16, 2026 at 4:23 pm

    You can combine the two ctors:

    Address::Address(const char* city=NULL, 
                     const char* street=NULL, 
                     int buildingNumber=0,
                     int apartmentNumber=0) 
    : m_city(city), 
      m_street(street), 
      m_buildingNumber(buildingNumber), 
      m_apartmentNumber(apartmentNumber)
     {}
    

    [The top-level const on buildingNumber and apartmentNumber accomplished nothing and attempt to move implementation information into the interface, so I remove them.]

    Of, if you really prefer:

    Address::Address(const char* city=NULL, 
                     const char* street=NULL, 
                     int buildingNumber=0,
                     int apartmentNumber=0) 
    {
        SetAddress(city,street,buildingNumber,apartmentNumber);
    }
    

    I generally prefer the former, but if SetAddress qualifies its inputs, it may be worthwhile. Of course, the suggestion to use std::string instead of pointers to char is a good one as well, but that’s a more or less separate subject.

    One other minor note: this does differ in one fundamental way from your original code. Your code required either 0 or 4 arguments to the ctor. This will accept anywhere from 0 to 4, arguments so a person could specify (for example) a city and street, but not a building number or apartment number. If it’s really important to you that attempts at using 1, 2 or 3 arguments be rejected, this approach won’t be useful to you. In this case, the extra flexibility looks like an improvement to me though — for example, if somebody lives in a single-family dwelling, it’s quite reasonable to omit an apartment number.

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

Sidebar

Related Questions

I'm having some difficulty writing this code in php. I want to create a
I'm writing a tokenizer that reads code from a file and am having difficulty
I'm having some difficulty with writing some jQuery code. What i'm trying to do
I'm having difficulty writing a program to solve this exercise from a Java text
I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that
I am having difficulty creating sessions for my mobile application that is written in
I'm having difficulty testing controllers. Original my controller for testing looked something like this:
I'm writing a plugin and having difficulty getting it to instantiate for multiple elements.
This is a homework problem that I am having difficulty with. The skeleton for
I am having some difficulty writing a function that will search through a directory

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.