Regarding the protected variables access


 
Thread Tools Search this Thread
Top Forums Programming Regarding the protected variables access
# 1  
Old 03-15-2012
Regarding the protected variables access

Hello forum,

I am siva working as programmer .I was blocked with the below issue so please help any of the forum memebers.

testve.h
Code:
class cv
{
 protected :
  struct state;
  state& m_state;
};

testVe.cpp
Code:
struct state
{
 m_size;
}

the above are 2 files which have the declaration and defenation of the struct. i am writing my own files ie test.cpp and test.h and my intension is to access the struct variables in my test.cpp file.

I am looking help for the above case from the Forum memebers.
Advance thanks


Thanks & Regards
Siva Ranganath.

Last edited by pludi; 03-15-2012 at 06:57 AM..
# 2  
Old 03-15-2012
Nothing except class cv and its friends or derived classes can access things inside class cv because you've made all members private. This is usually the case for classes actually; one makes class member functions for the purpose of access to give better control.

That structure looks confusing, unless you've #define'd m_size as something odd you've given it no name, only a type -- or perhaps vice versa. I think it should be

Code:
struct state
{
        variable_type m_size;
};

or
Code:
struct state
{
        m_size variablename;
};

# 3  
Old 03-15-2012
Also, your class looks a bit wrong too, you omitted the 'struct' from one and omitted the name from the other:

Code:
class cv
{
 protected :
  struct state mystate;
  struct state &m_state;
};

# 4  
Old 03-15-2012
Code:
#include "your-hdr-file.h" /*Header file where struct state is defined*/
class cv
{
 protected:
  struct state a;
  struct state &b;

public:
    cv(struct state &state_param):b(state_param)
    {
        ;
    }
    void access()
    {
        a.i = 1024;
        std::cout<<a.i<<"\n";
        std::cout<<b.i<<"\n";
    }
};

main()
{
    struct state s;
    s.i = 2048;

    cv obj1(s);
    obj1.access();
}

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to access the bash variables in csh

Hi, I am having a primary script which is Bash based. I am calling a csh script from it. Now, their are some variables defined in my bash script which i need in csh. I am unable to do so. Is it possible ? (2 Replies)
Discussion started by: vdhingra123
2 Replies

2. Cybersecurity

WebApp secure access to protected files/programs

Hello, I'm working on an embedded linux project that provides a devices that uses an IPSec VPN (using racoon) to connect back to base. The device also hosts a WebApp that allows admin users to change many aspect of the networking setup, including things like the VPN pre-shared-key, IP addresses... (1 Reply)
Discussion started by: salukibob
1 Replies

3. Shell Programming and Scripting

problem in access in array variables

hi all, its me again!!! i've requirement like this: i want to create a file & an array with its name having the filename as its substring. here is the test script!! #!/bin/bash touch $1 declare -a $1_rec; echo -n "$1_rec: " read $1_rec; echo $]; now see output: this is when i enter... (7 Replies)
Discussion started by: tprayush
7 Replies

4. Shell Programming and Scripting

Access variables from profile file

Hi all, I hav created one profile file in which i have declared some variables i want to knw how can i access these variables into my xyz.ksh file when i am trying to do " echo $variablename " its giving me an empty line i have done export variable also in the profile file. Thanks in... (6 Replies)
Discussion started by: navi
6 Replies

5. Shell Programming and Scripting

Access Awk Variables Outside Scope

My awk script searches for specified patterns in a text file and stores these values into mem variables. Once this is done I want to Insert these values into a table. How can I avail of the variable values outside the scope of awk script.... One method that I have tried is to write the... (7 Replies)
Discussion started by: Amruta Pitkar
7 Replies

6. Shell Programming and Scripting

How to access variables across scripts

Hi All, I have declared a variable in script1 and assign a value for it. In script2 i'll call script1 and then I want the value of variables set in script1. I have tried with export, but in vain. How can I achive this? Below is the two scripts. --script1 #!/usr/bin/ksh echo $1... (1 Reply)
Discussion started by: javaDev
1 Replies

7. UNIX for Advanced & Expert Users

Access Awk Variables Outside Scope

Sorry in the wrong forum. Moving this to right forum. (2 Replies)
Discussion started by: Amruta Pitkar
2 Replies

8. UNIX for Dummies Questions & Answers

Read variables from Access table

Hi! I've just started learning shell scripting, and have been somewhat 'thrown in at the deep-end and told to swim' so excuse my complete lack of knowledge and ignorance, but here goes... I've been given a unix script to 'tidy up'. Basically the script consists of the few lines of code being... (2 Replies)
Discussion started by: Sn33R
2 Replies
Login or Register to Ask a Question