0

Good day everyone! I would just like to ask if how it is possible to access a private structure inside a class?

My code looks like this:

class VideoClass{
       
   private:
       struct vidstruct {
            int Video_ID;  
            string movietitle;
            string genre;
            string prod;
            int numberOfCopies;
            string MovImg_name;
       };
       
       
   public:
       VideoClass();
       // ~VideoClass();
       void insertVideo(vidstruct info);
       void rentVideo(int rv); // 
       void returnVideo(); // ---
       void showDetails(int sd); 
       void validateVideo(); //
       void displayVideo();
};

What I wanted to is to access the vidstruct (name of the structure) to any parts of my program. Thankyou for your kind answers in advance :)

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
Salieri_
  • 11
  • 1
  • 1
    That goes directly against `private:`, doesn't it? Why not make it public then? – Quimby Mar 14 '21 at 09:09
  • I'm not sure if I should make it public since I want the attributes inside the structure to be at least secure by making it private. – Salieri_ Mar 14 '21 at 09:14
  • 1
    What do you mean by secure? Do you consider them an implementation detail? In that case `private` is correct, but then you should not be using the structure from the rest of the program. Can you please elaborate on how you want to use `vidstruct`? E.g. you have public `insertVideo` which is unusable right now. – Quimby Mar 14 '21 at 09:22
  • There is no easy way how to make `private` members accessible because by writing `private` you are saying they are not meant to be. So, take a moment to figure out what purpose does `vidstruct` serve. Maybe `insertVideo` could take the attributes directly? Maybe you need two structures? Maybe making `vidstruct` part of the API is the way to go. – Quimby Mar 14 '21 at 09:25
  • Does this answer your question? [Should I use public or private variables?](https://stackoverflow.com/questions/14399929/should-i-use-public-or-private-variables) – Oliort UA Mar 15 '21 at 01:17

1 Answers1

-1

The fact that your member vidstruct is declared as private means that it is not accessible outside of the class. If it is meant to be used outside of the class You could declare it simply outside of it.

Here would be a snippet example (printing 'movie test'):

#include <iostream>

using namespace std;

typedef struct {
    int Video_ID;  
    string movietitle;
    string genre;
    string prod;
    int numberOfCopies;
    string MovImg_name;
} vidstruct;

class VideoClass{
    public:
       VideoClass() {
       };
       // ~VideoClass();
       void insertVideo(vidstruct info) {
           cout << info.movietitle;
       };
       void rentVideo(int rv); // 
       void returnVideo(); // ---
       void showDetails(int sd); 
       void validateVideo(); //
       void displayVideo();
};

int main()
{
    vidstruct x;
    x.movietitle = "movie test";

    VideoClass videoTest;
    videoTest.insertVideo(x);
    return 0;
}
Benzard
  • 1
  • 1