PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label private. Show all posts
Showing posts with label private. Show all posts

Thursday, October 20, 2022

[FIXED] How to get names of all private data members in a class

 October 20, 2022     class, datamember, java, private     No comments   

Issue

I need a function that will return the names of all the private data members in my class as strings (perhaps in an array or list?), where each string is the name of a private, non final data member in my class. The non final condition is optional, but it would be nice.

1) Is this even possible? I think there is a way to retrieve all method names in a class, so I think this is possible as well.

2) I know I am asking for a hand out, but how do I do this?

EDIT

I have NO idea where to begin.

It seems java.lang.reflect is a good place to begin. I have started researching there.


Solution

This should do the trick. Basically you got in a List all the fields of your class, and you remove the one who are not private. :

public static void main(String [] args){
    List<Field> list = new ArrayList<>(Arrays.asList(A.class.getDeclaredFields()));

    for(Iterator<Field> i = list.iterator(); i.hasNext();){
        Field f = i.next();
        if(f.getModifiers() != Modifier.PRIVATE)
            i.remove();
    }
    for(Field f : list)
        System.out.println(f.getName());
}

Output :

fieldOne
fieldTwo

Class A :

class A {
    private String fieldOne;
    private String fieldTwo;

    private final String fieldFinal = null;

    public char c;
    public static int staticField;
    protected Long protectedField;
    public String field;
}


Answered By - user2336315
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to access private data members outside the class without making "friend"s?

 October 20, 2022     c++, class, data-members, encapsulation, private     No comments   

Issue

I have a class A as mentioned below:-

class A{
     int iData;
};

I neither want to create member function nor inherit the above class A nor change the specifier of iData.

My doubts:-

  • How to access iData of an object say obj1 which is an instance of class A?
  • How to change or manipulate the iData of an object obj1?

Note: Don't use friend.


Solution

You can't. That member is private, it's not visible outside the class. That's the whole point of the public/protected/private modifiers.

(You could probably use dirty pointer tricks though, but my guess is that you'd enter undefined behavior territory pretty fast.)



Answered By - Mat
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] What is handy way to modify the private class data members without writing explicit setters? Are templates useful?

 October 20, 2022     c++, c++14, datamember, private, templates     No comments   

Issue

I have a class with data members.

class Sph
{
public:
    Sph( float radius , float segments , const Shader& shader );
    Sph(const Sph& geometry);
    Sph& operator = (const Sph& geometry);
    ~Sph();
    void init();
    void CleanUp();
    void draw();
    void CreateUI(QFormLayout* layout);
    std::vector< Sum_Vertices > GetVertices();

private:
    float Radius,Segments;
    bool isInited;
    unsigned int m_VAO, m_VBO , m_IBO;
    int iNumsToDraw;
    bool isChanged;
    Shader shader;
    int indexCount;
};

in order to change the data of the class I have to write individual methods.

void SetRadius( float radius )
{
   this->Radius = radius;
}

Is it possible to write a templated function which can change different data members of the class?


Solution

Since the variables are different by names, the template may not help here.


This is a classic case, where you can use macros to your advantage & ease.

Definition:

#define GET_SET(X) X; \ 
    public: const decltype(X)& get_##X() const { return X; } \ 
            void set_##X(const decltype(X)& value) { X = value; } private:

Utility:

class Sph
{
   ...
private:
  float GET_SET(Radius);
  float GET_SET(Segments);
  ...
};

Usage:

Sph sph;
sph.set_Radius(3.3);
cout << sph.get_Radius() << "\n";

Demo.



Answered By - iammilind
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, July 8, 2022

[FIXED] Why can't I access private members of class Box in operator<<?

 July 08, 2022     c++, class, iostream, operator-keyword, private     No comments   

Issue

Why can't I access private functions of class Box in ostream& operator<<(ostream& out, const Box& B){cout << B.l << " " << B.b << " " << B.h << endl; }?

#include<bits/stdc++.h>
using namespace std;

class Box{
    int l, b, h;
    public:
      Box(){
          l=0;
          b=0;
          h=0;
      }
      Box(int length, int breadth, int height){
          l=length;
          b=breadth;
          h=height;
      }
      bool operator < (Box &B){
          if(l < B.l)return 1;
          else if(b < B.b && l==B.l)return 1;   
          else if(h< B.h && b== B.b && l==B.l)return 1;
          else return 0;
      }

    };
    ostream& operator <<(ostream& out, const Box& B){
        cout << B.l << " " << B.b << " " << B.h ;
        return out;
    }

Solution

The problem is that you don't have any friend declaration for the overloaded operator<< and since l, b and h are private they can't be accessed from inside the overloaded operator<<.

To solve this you can just provide a friend declaration for operator<< as shown below:

class Box{
    int l, b, h;
    //other code here as before
    
//--vvvvvv----------------------------------------->friend declaration added here
    friend ostream& operator <<(ostream& out, const Box& B);

};
//definition same as before
ostream& operator <<(ostream& out, const Box& B){
    cout << B.l << " " << B.b << " " << B.h ;
    return out;
}

Working demo



Answered By - Anoop Rana
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing