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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:20:44+00:00 2026-06-15T05:20:44+00:00

Inside main.cpp class vivek { int i; float d; public: vivek() { i =

  • 0

Inside main.cpp

class vivek
{
    int i;
    float d;
public:
    vivek()
    {
        i = 4;
        d = 4.44;
    }
    ~vivek()
    {
        cout << "destructor" << i;
    }
    int get()
    {
        return i;
    }
};

int main()
{
    vivek *viku = new vivek;
    vivek *p;

    p = viku;
    cout << (*(int*) (p));
    getchar();

    return 0;
}

Through above code i can able to access variable i , but i want to know how can i access the variable d .

I know this concept shall not be advisable since it violates encapsulation .. But i just want to know how we can do this ?

  • 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-15T05:20:45+00:00Added an answer on June 15, 2026 at 5:20 am

    While it’s extremely hacky, it is possible, because vivek is a standard-layout struct. This means you can do this:

    #include <iostream>
    
    // copied from original
    class vivek {
      int i;
      float d;
    public:
      vivek() {i = 4; d = 4.44; }
      ~vivek() { cout<<"destructor"<<i; }
      int get() { return i; }
    };
    
    struct vivek_sneaky_accessor {
      int i;
      float d;
    };
    
    int main() {
      vivek v;
      vivek_sneaky_accessor *acc = reinterpret_cast<vivek_sneaky_accessor*>(&v);
      std::cout << acc->d;
      return 0;
    }
    

    This relies on [class] points 7 and 8 (definition of standard-layout struct) and on [class.mem] points 17 and 20 (layout-compatibility of standard-layout structs).

    Disclaimer I am in no way advocating this as a reasonable thing to do. I am just saying that it is actually possible.

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

Sidebar

Related Questions

#include <iostream> using namespace std; class B { public: int getMsg(int i) { return
If I create an object inside of the main scope: INDEX.PHP: $db = new
I have the following source code : // main.cpp #include a.h int main() {
I have program.cpp which contains main function. There is a class Tracker which is
Given the following code: class monomial { public: mp_real coe; int exp; monomial *next;
I have two files: // event_test_delete.cpp #include <event.h> int main() { event_base* ev; ev
I'm trying to declare a class object inside a header but I can't get
Inside my main folder, I have multiple sub-folders and each sub folder contains multiple
Scenario I have a Windows Forms Application. Inside the main form there is a
I have two MAIN DIVs inside a DIV which is a PART of the

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.