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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:17:55+00:00 2026-06-15T14:17:55+00:00

I have a base Image class with const field members: class Image { protected:

  • 0

I have a base Image class with const field members:

class Image {
protected:
    const int width;
    const int height;

public:
    virtual ~Image();
    const int getWidth() const;
    const int getHeight() const;

protected:
    Image(const int width, const int height);
};

This means in the constructor, I have to initialize the width and height in the initialization list like so:

Image::Image(const int width, const int height) :
        width(width), height(height) {
}

Now I intend to subclass this such that I can load an image by providing the filepath to the subclass (so calling code does not have to worry about loading the image). This class would look similar to the following:

class GlImage: public Image {
private:
    const GLuint textureId;
    const int textureWidth;     // power of 2 width
    const int textureHeight;    // power of 2 height
    const double textureCoordinateX;
    const double textureCoordinateY;

public:
    GlImage(const string path); // is this constructor possible?
    virtual ~GlImage();
    const double getTextureCoordinateX() const;
    const double getTextureCoordinateY() const;

private:
    // what do we use for initialization functions?
};

However, we can see here that
– the image has to be loaded before we can obtain the width/height
– there are fields in the subclass that need to be initialized in the initialization list as well

How can the constructor of the subclass be set up such that all of these fields can be initialized?

It’s not possible to introduce another object in the subclass which captures all of the field data, load this as the first object in the initialization list, and re-extract the values out for all the other fields as the base class needs the width/height (which aren’t available if the image loading logic is in the derived class).

Is the only way out to remove the const modifiers and initialize the fields in the constructor?

  • 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-06-15T14:17:56+00:00Added an answer on June 15, 2026 at 2:17 pm

    The problem is that you can’t call a base class constructor from derived class ctor initializer list, as you don’t know the dimensions, right?

    Why don’t you create a static method on derived class?

    class GlImage: public Image {
        ....
        static GlImage LoadFromFile(const string& path)
        {
             // read image data and other info
             int w = ...
             int h = ...
             return GlImage(w, h, ....) 
             // of course, you would need a different ctor in derived class
             // it can even be private
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class: class motorcycle { public: virtual int speed() { return
I have a class template<typename T> class RGBHistogramTrait { public: RGBHistogramTrait(const QImage &image, RGBHistogram<T>
I have a class, called Sprite: public abstract class Sprite { protected Texture2D Image
I have a base class like this: public class BaseResponse { public string ErrorMessage
I have a base class that does calculations on image sizes. I'm deriving a
I have a model Image: class Image < ActiveRecord::Base attr_accessible :description, :name, :size, :image,
I have this Pin Model: class Pin < ActiveRecord::Base belongs_to :user belongs_to :image accepts_nested_attributes_for
I have a base-class called Element. Some other classes (like Label and Image) both
I have a model Image: class Image < ActiveRecord::Base attr_accessible :date, :description, :name, :quality,
I have 2 models (Book & Image) class Book < ActiveRecord::Base has_many :images accepts_nested_attributes_for

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.