Something bizarre is happening... a single variable is holding multiple values

Currently, I am in the process of developing a C++ program that involves dealing with CSS. However, I have encountered a perplexing issue that has me completely stumped.

The problem lies within my class structure setup where I have a class called HTMLObject that inherits from another class named CSS:

struct Border {
    string bottom;
    string bottom_color;
    string bottom_left_radius;
    string bottom_right_radius;
    string bottom_style;
    //... many more strings 
};

struct Background {
    string color;
    string image;
    string position;
    string size_;
    string repeat;
    string origin;
    string clip;
    string attachment;
};

struct Margin {
    string top, right, bottom, left;
};

struct Padding {
    string top, right, bottom, left;
};

class CSS {
private:

public:
    string position, display, height, width;
    string top, right, bottom, left;
    string color;
    string min_width, min_height, max_width, max_height;
    string overflow, opacity, text_align, z_index;
    Margin margin;
    Padding padding;
    Background background;
    Border border; 
    Font font; 
};

This is followed by the declaration of the CSS HTMLObject:

class HTMLObject : public CSS
{
public:
    //..various members and methods
    CSS css;
    string GetCSS()
    {
         cout << "instance address: " << this << " " << 
                 this->css.background.image << this->css.color << " " << 
                 this->css.border.bottom << endl;
         
         //more code to be added here before returning a string
    }
};

Furthermore, there are additional classes that inherit from HTMLObject:

class cClass1 : public HTMLObject
    {
    private:


    public:
        cClass1() : HTMLObject("instantiated"){}
    };
typedef cClass1* Class1;

Lastly, we have our main function implementation:

Class1 b = new cClass1();
b->css.color = "red";
b->css.border.bottom = "3px";
b->background.image = "-webkit-linear-gradient(#ffffff 0%, #F6F6F6 30%, #F3F3F3 45%, #EDEDED 60%, #eeeeee 100%)";
cout << "Var address:" << b << " " << b->background.image << endl << endl;
cout << b->GetCSS() << endl;

cout << "GetCSS() finished.\n";

Upon running the program, the output I'm receiving is not what I anticipated, causing some frustration on my end!

Var address: 0x55e3b0b83ae0 -webkit-linear-gradient(#ffffff 0%, #F6F6F6 30%, #F3F3F3 45%, #EDEDED 60%, #eeeeee 100%)

instance address: 0x55e3b0b83ae0 red 3px

GetCSS() finished.

In case you want to take a look at the screenshot of the output, you can do so here.

Note: I am using GCC (GNU Compiler) and faced this issue on both Ubuntu and MacOSX systems.

Answer №1

HTMLObject is a subclass of CSS and includes a CSS property...

Therefore, there are 2 CSS data:

this->css.background.image is different from this->background.image.

You might want to consider removing the inheritance.

class HTMLObject 
{
public:
    CSS css;
    string GetCSS();
    // ...
};

Also, correct the code in main (change background.image to css.background.image):

cClass1 b;
b.css.color = "red";
b.css.border.bottom = "3px";
b.css.background.image = "-webkit-linear-gradient(#ffffff 0%, #F6F6F6 30%, #F3F3F3 45%, #EDEDED 60%, #eeeeee 100%)";
cout << "Variable address:" << &b << " " << b.css.background.image << endl << endl;
cout << b.GetCSS() << endl;

cout << "GetCSS() completed.\n";

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for implementing automatic line wrapping in a PDF document using React-PDF

I am experiencing an issue with a PDF rendering where very long words are not wrapping onto a new line, instead overflowing the container and disappearing off the page. Below is the setup causing this problem. The styles are listed followed by the actual ...

The sidebar appears to be hovering above the footer section

I am currently working on creating a sidebar that sometimes ends up being longer than the main content section. You can check out my demo here: http://jsfiddle.net/y9hp4evy/1/ and another case here: http://jsfiddle.net/y9hp4evy/2/ (If I add a height to th ...

The rotation feature for legends in Highcharts does not function properly on Internet Explorer 10 and 11 if the document mode is

When using Internet Explorer 10 or 11, rotation works perfectly fine if we select edge mode, as shown in the image. This information was taken from the High Chart Demo Charts. However, if we select Document mode 8 in IE 10 or IE 11, the rotation does not ...

Automatically trigger a PHP reload/refresh based on user input

My website has a form that populates a table with data. Currently, after the user submits the data, they have to manually refresh the page in order to see the new data displayed. Is there a way to automatically refresh the table every time the user hits ...

How can I dynamically set the width of a span element in HTML?

Hello there! As a beginner in html and angularjs, I'm experimenting with dynamically assigning span width based on an angular js response. <div class="statarea" ng-repeat="x in names"> <div class="ratingarea"> <div class=" ...

Using Bootstrap's card component, design a grid layout for your website

I am attempting to construct a grid using the Bootstrap 4 card component. After reviewing the documentation and utilizing the card-deck feature, I aim to have each row consist of two columns with the behavior similar to col-12 col-md-6. Additionally, the s ...

The element on the page is not taking up the full space available

After applying the CSS code below in an attempt to fill my page entirely with an image from my computer, I ended up with unexpected results: #background { background-image: url("anFtp"); position: fixed; height:100%; width:100%; z-inde ...

creating divs inside a parent div with varying heights and using auto margins

Can anyone help me with this code snippet? <div style="position: relative;"> /***main***/ <div style="top:0"> /*****Content1****/ </div> <div> /*****Content2****/ </div> <div> ...

Creating line breaks for ToolTip titles in Material-UI can be achieved by using the appropriate syntax and

I am currently working with the ToolTip component and I have a query regarding displaying two lines for the title text - one line for each language rather than having them displayed on a single line. Is it possible to achieve this and how can I style the c ...

Manipulating an event on an InkCanvas

Why does the InkCanvas only emit the ManipulationDelta event when the InputDeviceType of InkPresenter is set to CoreInputDeviceTypes::Pen? Changing it to include more types like Mouse, Touch, and Pen prevents the event from being triggered. What could be ...

Is there a way to compel the browser to re-cache an image?

In my website, each user has a cover image on their profile page with the constant name main_cover.jpg. The issue I am facing is that when a user uploads a new image, the old image remains cached in the browser and I have to refresh the page (ctrl+f5) to ...

Avoiding interference with adjacent elements caused by long content in a div

Hey there, I could really use some help with my CSS layout, I'm pretty new to CSS and Flexbox and I'm trying to create a simple layout. The issue I'm running into is how to stop long content inside a pre tag from pushing other divs. Here& ...

The text feature for the Google Places API is not displaying properly within the MUI TextField

For address auto-generation, I am currently utilizing the Google Places API. I have a regular input tag in my HTML where users can type their input and see Google Places suggestions in the dropdown - all working perfectly. However, when I switch to using m ...

Troubleshooting a live chat box for CSS alignment issues

Need help with aligning a live chat box on every webpage? Check out this example for reference: You can find my code here: https://jsfiddle.net/fn77d0de/2/ ...

Adjusting image width using jQuery

I have been experimenting with creating a Webgl hover effect for an image. The effect works well, but now I am trying to specify a width for the image within jQuery. new hoverEffect({ parent: document.querySelector('.ticket'), intensity1: 0. ...

Having trouble with my ajax code that reads a file into a div. I must be overlooking a small detail

Despite the simplicity of my task, I'm encountering an issue. Perhaps there is something crucial that I am overlooking. The objective is to retrieve the content of a text file using AJAX and display it within a div. While I can successfully write to ...

Boostrap: A guide to positioning a button on top of an image while resizing

Need help figuring out how to position and resize a button over an image on your website? Currently, the button is moving when you resize the browser window. You've tried using percentages in the CSS without success. What should you do next? <div ...

Data containing visual content is not being successfully transferred to the MySQL database

I am having an issue where the code is not sending data to the database after clicking on submit. I have also attempted using the POST method with no success. These are the table names in my database: id (AI), image, name, dob, fathername, fatheroccu ...

Having trouble with PDF formatting using PDFkit and Wkhtmltopdf - the table is getting pushed onto the next page. Any tips for fixing this issue?

I've encountered a problem while attempting to generate PDFs in Python Django using PdKit and Wkhtmltopdf. Specifically, when there are more than 10 items in the table, it automatically moves to the next page. I have tried several solutions including: ...

Modify Qt: adjusting the thickness of the font

I need my text in QLabel to be a blend of bold and normal style. I believe adjusting the font-weight should solve this issue. While researching Qt documentation, I discovered two ways to modify font-weight: From the C++ side with the QFont::setWeight() m ...