Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #12800

Re: Four variables share one string?

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Four variables share one string?
Date 2011-12-24 18:21 -0500
Organization A noiseless patient Spider
Message-ID <jd5mqa$804$1@dont-email.me> (permalink)
References <9cbdac30-eb33-45cf-8fe5-661f799c27c2@a17g2000yqj.googlegroups.com>

Show all headers | View raw


On 12/24/2011 5:54 PM, Nephi Immortal wrote:
> 	Is there a way how to create ONE four bytes in memory storage and
> four variables can access one str directly?  I can do that without
> static const and C++ Compiler will be able to compile without any
> problem, but static is disallowed in union.
> 	I do not want to use pointer variable to str because I prefer “direct
> memory” variable instead of “indirect memory” variable.
>
> union
> {
> 	static const char str[ 5 ] = “ABCD”;
> 	struct Letter
> 	{
> 		static const char a;
> 		static const char b;
> 		static const char c;
> 		static const char d;
> 	} letter;
> };
>
> 	char A = letter.a; // read A from str and put it in A variable
> 	char B = letter.b; // read A from str and put it in B variable
> 	char C = letter.c; // read A from str and put it in C variable
> 	char D = letter.d; // read A from str and put it in D variable

You could try

    struct Foo {
        static const char str[5]; // initialization has to be outside
        static const char &a;
        static const char &b;
        static const char &c;
        static const char &d;
    };

    const char Foo::str[] = { "ABCD" };
    const char &Foo::a = Foo::str[0];
    const char &Foo::b = Foo::str[1];
    const char &Foo::c = Foo::str[2];
    const char &Foo::d = Foo::str[3];

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Four variables share one string? Nephi Immortal <immortalnephi@gmail.com> - 2011-12-24 14:54 -0800
  Re: Four variables share one string? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-24 18:21 -0500
    Re: Four variables share one string? Nephi Immortal <immortalnephi@gmail.com> - 2011-12-24 18:16 -0800
      Re: Four variables share one string? Kalle Olavi Niemitalo <kon@iki.fi> - 2011-12-25 14:20 +0200
        Re: Four variables share one string? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-25 08:54 -0500
          Re: Four variables share one string? Leigh Johnston <leigh@i42.co.uk> - 2011-12-25 14:04 +0000
          Re: Four variables share one string? Michael DOUBEZ <michael.doubez@free.fr> - 2011-12-26 02:02 -0800
          Re: Four variables share one string? Nephi Immortal <immortalnephi@gmail.com> - 2011-12-26 07:50 -0800
            Re: Four variables share one string? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-26 18:34 -0500

csiph-web