473,473 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

access to structure members

What is the syntax to access members of a structure without
explicitly naming the structure in every access?

struct mytype {
int a;
char* b;
long c;
} IT;

How can I access the structure members in a way similar to the old pascal
USING statement?

using IT (
a=5;
b="hello";
c=45l;
);
I cannot remember the c++ syntax to accomplish this.
May 31 '07 #1
6 2997
no***@all.com wrote:
What is the syntax to access members of a structure without
explicitly naming the structure in every access?

struct mytype {
int a;
char* b;
long c;
} IT;

How can I access the structure members in a way similar to the old pascal
USING statement?

using IT (
a=5;
b="hello";
c=45l;
);

1. The old Pascal construct was a "with" statement.
2. There is no way to do what you want. Why do you want to do so? Your
code is more maintainable without it.
May 31 '07 #2
no***@all.com wrote:
What is the syntax to access members of a structure without
explicitly naming the structure in every access?

struct mytype {
int a;
char* b;
long c;
} IT;

How can I access the structure members in a way similar to the old
pascal USING statement?

using IT (
a=5;
b="hello";
c=45l;
);
I cannot remember the c++ syntax to accomplish this.
Nobody can. There is no such syntax. Non-static data members
require an instance of the class to be accessed. You can only
access them without additional qualification inside a non-static
member function, where there is an implicit "this->".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 31 '07 #3
On May 31, 11:32 am, n...@all.com wrote:
What is the syntax to access members of a structure without
explicitly naming the structure in every access?

struct mytype {
int a;
char* b;
long c;

} IT;

How can I access the structure members in a way similar to the old pascal
USING statement?

using IT (
a=5;
b="hello";
Its time to unlearn the lies:
member b is a pointer to a single character. char* b cannot store
anything else.
Relying on a pointer to a single char to store an array of characters
is doomed to fail.
The compiler has not allocated or reserved memory to store an array of
chars.
You'ld have to store the null-terminated character sequence using a
buffer (ie: char b[128];).
However - there is a better solution: std::string (its dynamic and
packed with features).
c=45l;
);

I cannot remember the c++ syntax to accomplish this.
Using a function or a constructor. IT is not a type/struct/class, its
an instance of mytype.
Consider a primitive array: mytype arr[100];
Which one of those 100 instances' member's did you want to modify?
You need to tell the program which one to act on. (ie:
array[0].set(...);)
But wait a minute, why set anything when you can allocate AND
initialize private members simultaneously at construction time?

#include <iostream>
#include <string>

struct mytype {
private:
int a;
std::string b;
long c;
public:
// default ctor + init list
mytype()
: a(0), b("default string"), c(0) { }
// parametized ctor + init list
mytype(const int n,
const std::string& s,
const long l)
: a(n), b(s), c(l) { }
// friend op<< for output
friend std::ostream& operator<<(std::ostream& os, const mytype& t)
{
os << "a = " << t.a;
os << "\nb = " << t.b;
os << "\nc = " << t.c;
return os;
}
};

int main()
{
mytype instance(5, "hello", 451); // done
std::cout << instance << std::endl;

mytype array[100];
std::cout << array[0] << std::endl;
}

// Note: all 100 instances in that array[] are already set.

May 31 '07 #4
On May 31, 11:32 am, n...@all.com wrote:
What is the syntax to access members of a structure without
explicitly naming the structure in every access?

struct mytype {
int a;
char* b;
Its time to unlearn the lies:
member b is a pointer to a single character. char* b cannot store
anything else. Relying on a pointer to a single char to store an array
of characters
is doomed to fail. The compiler has not allocated or reserved memory
to store an array of
chars. You'ld have to store the null-terminated character sequence
using a
buffer (ie: char b[128];).
However - there is a better solution: std::string (its dynamic and
packed with features).
long c;

} IT;

How can I access the structure members in a way similar to the old pascal
USING statement?

using IT (
a=5;
b="hello";
c=45l;
);

I cannot remember the c++ syntax to accomplish this.
Using a function or a constructor. IT is not a type/struct/class, its
an instance of mytype. Consider a primitive array:
mytype arr[100];
Which one of those 100 instances' member's did you want to modify? You
need to tell the program which one to act on. (ie: array[0].set(...);)
But wait a minute, why set anything when you can allocate AND
initialize private members simultaneously at construction time?

#include <iostream>
#include <string>

struct mytype {
private:
int a;
std::string b;
long c;
public:
// default ctor + init list
mytype()
: a(0), b("default string"), c(0) { }
// parametized ctor + init list
mytype(const int n,
const std::string& s,
const long l)
: a(n), b(s), c(l) { }
// friend op<< for output
friend std::ostream& operator<<(std::ostream& os, const mytype& t)
{
os << "a = " << t.a;
os << "\nb = " << t.b;
os << "\nc = " << t.c;
return os;
}

};

int main()
{
mytype instance(5, "hello", 451); // done
std::cout << instance << std::endl;

mytype array[100];
std::cout << array[0] << std::endl;

}

// Note: all 100 instances in that array[] are already set.

May 31 '07 #5
no***@all.com wrote:
What is the syntax to access members of a structure without
explicitly naming the structure in every access?

struct mytype {
int a;
char* b;
long c;
} IT;

How can I access the structure members in a way similar to the old pascal
USING statement?

using IT (
a=5;
b="hello";
c=45l;
);
I cannot remember the c++ syntax to accomplish this.

couldn't you use #define and rename each one to an alias? Not saying it
is a good way, just a possibility (I think). Feel free to correct me if
I am wrong.
May 31 '07 #6
Salt_Peter wrote:
> b="hello";

Its time to unlearn the lies:
member b is a pointer to a single character. char* b cannot store
anything else.
Relying on a pointer to a single char to store an array of characters
is doomed to fail.
The compiler has not allocated or reserved memory to store an array of
chars.
On the contrary: the compiler allocated and reserved memory for the
string "hello", including its terminating null character. It's perfectly
appropriate to refer to that array through a char* (although a const
char* would be preferable). Pointers often point to a single object, but
they also often point to the first element of an array.
You'ld have to store the null-terminated character sequence using a
buffer (ie: char b[128];).
However - there is a better solution: std::string (its dynamic and
packed with features).
That depends on what the problem is. There's nothing in the original
question that supports a judgment about the relative merits of char*
versus std::string. That's not important, though, since it has nothing
to do with what was actually asked.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 31 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
52
by: Neil | last post by:
We are running an Access 2000 MDB with a SQL 7 back end. Our network guy is upgrading to Windows Server 2003 and wants to upgrade Office and SQL Server at the same time. We're moving to SQL Server...
9
by: gk245 | last post by:
I have something like this: struct block { int x; int y; float z; }
2
by: A n g l e r | last post by:
Hello everybody. I've got such an aching me question concerning fast & well optimized access to members of a structure (or class) and ways that VC compiler handles it. Let's imagine the...
6
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
2
by: DhaneshNair | last post by:
I have created a COM server in which a collection of vectors need to be exposed to C# sharp client . Each vector contains a array of structures and this vector need to be embedded inside a...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
0
by: abarun22 | last post by:
Hi I would like to know if there are any ways to access the members of a nested structure inside python. I use SWIG as interface for C. My structure comes as follows. struct SA{ int nb_dep,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.