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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:52:05+00:00 2026-05-14T00:52:05+00:00

When should I inline a member function and when should I use member initializers?

  • 0

When should I inline a member function and when should I use member initializers?

My code is below.. I would like to modify it so I could make use some inline when appropriate and member initializers:

#include "Books.h"

Book::Book(){
  nm = (char*)"";
  thck = 0;
  wght = 0;
}

Book::Book(const char *name, int thickness, int weight){
  nm =  strdup(name);
  thck = thickness;
  wght = weight;
}

Book::~Book(){

}

const char* Book::name(){
 return nm;
}

int Book::thickness(){
 return thck;
}

int Book::weight(){
 return wght;
}

//
// Prints information about the book using this format:
// "%s (%d mm, %d dg)\n"
//
void Book::print(){
  printf("%s (%d mm, %d dg)\n", nm, thck, wght);
}


Bookcase::Bookcase(int id){
 my_id = id;
 no_shelf = 0;
}

int Bookcase::id(){
 return my_id;
}

Bookcase::~Bookcase(){
  for (int i = 0; i < no_shelf; i++)
    delete my_shelf[i];
}

bool Bookcase::addShelf(int width, int capacity){
  if(no_shelf == 10)
    return false;
  else{
    my_shelf[no_shelf] = new Shelf(width, capacity);
    no_shelf++;
    return true;
  }
}

bool Bookcase::add(Book *bp){
 int index = -1;
 int temp_space = -1;
 for (int i = 0; i < no_shelf; i++){
    if (bp->weight() + my_shelf[i]->curCapacity() <= my_shelf[i]->capacity()){
       if (bp->thickness() + my_shelf[i]->curWidth() <= my_shelf[i]->width() && temp_space < (my_shelf[i]->width() - my_shelf[i]->curWidth())){
        temp_space = (my_shelf[i]->width()- my_shelf[i]->curWidth());
        index = i;
          }
    }
 }

 if (index != -1){
    my_shelf[index]->add(bp);
    return true;
 }else
    return false;

}

void Bookcase::print(){
 printf("Bookcase #%d\n", my_id);
 for (int i = 0; i < no_shelf; i++){
    printf("--- Shelf (%d mm, %d dg) ---\n", my_shelf[i]->width(), my_shelf[i]->capacity());
    my_shelf[i]->print();
 }
}
  • 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-14T00:52:05+00:00Added an answer on May 14, 2026 at 12:52 am

    Short member functions that are called frequently are good candidates for inlining. In order to make the member function “inlinable,” you need to define it in the header file (either in the class definition itself, or below the class definition using the inline keyword).

    You should always use constructor initializer lists to initialize member data. For user-defined types, this can make a substantial performance difference in some cases. Your constructor should look like this:

    Book::Book() : nm(""), thck(0), wght(0) { }
    

    Here is a pretty good explanation of why to use initializer lists.

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

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer PCL XL, aka PCL version 6, does not support CMYK… May 14, 2026 at 11:22 pm
  • Editorial Team
    Editorial Team added an answer Well if that JavaScript file defines a specific variable (or… May 14, 2026 at 11:22 pm
  • Editorial Team
    Editorial Team added an answer If the search object isn't huge in size, then go… May 14, 2026 at 11:22 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.