473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const problems with one class referencing another

Hi folks,

currently I'm writing image classes for easier handling of Intel's IPP library.

My idea is to have to different classes. One class that represents a complete
image and deals with all the memeory management stuff, and another class, that
is only a tile of the image. Let's call the first MemoryImage, and the second
ReferenceImage.

My code works well for creating non const and const reference images to a non
const memory image. Unfortunately, it doesn't work for const memory images.

The following code illustrates my problem. It's neither stylistically nor
functionally perfect (in fact it's neglecting the identical byte offsets
between consecutive images lines, even though the image widths might be
different.
class MemImage {

public:

// Create image.
MemImage(int width, int height) : m_data(0) {

// check dimensions and allocate aligned memory
}

// Get width of image.
int getWidth() const { return m_width; }

// Get height of image.
int getHeight() const { return m_height; }

// Get pointer to image data.
unsigned char* getData() { return m_data; }

// Get const pointer to image data.
const unsigned char* getData() const { return m_data; }

protected:

unsigned char * m_data;
int m_width;
int m_height;

}; // class MemImage
class RefImage {

public:

RefImage(MemIma ge &image, int x, int y, int width, int height)
: m_data(image.ge tData()+y*image .getWidth()+x), m_width(width), m_height(height )
{}

// Get pointer to image data.
unsigned char* getData() { return m_data; }

// Get const pointer to image data.
const unsigned char* getData() const { return m_data; }

protected:

unsigned char *m_data;
int m_width;
int m_height;

}; // class RefImage
With the example code above, its possible to do:

void f()
{
MemImage m(400,300);
RefImage r(m, 40, 30, 200, 150);
const RefImge c(m, 40, 30, 200, 150);
}

It's also possible to change image data of m through r.getData()

The code breaks with:
void g()
{
const MemImage m(400,300);
const RefImge c(m, 40, 30, 200, 150);
}

Changing the constructor of RefImage to

RefImage(const MemImage &image, int x, int y, int width, int height)
: m_data(image.ge tData()+y*image .getWidth()+x), m_width(width), m_height(height )
{}

fails due to the initialization of unsigned char m_data with a const unsigned char.

Changing the member m_data of RefImage to const unsigned char, the constructor works,
but it's no longer possible to have write access to non const MemImages via a RefImage.

Can anyone break this vicious circle?

Thanks in advance,

Torsten

Aug 10 '07 #1
3 1603
"Torsten Wiebesiek" <wi*******@tnt. uni-hannover.dewrot e in message
news:f9******** **@registered.m otzarella.org.. .
Hi folks,

currently I'm writing image classes for easier handling of Intel's IPP
library.

My idea is to have to different classes. One class that represents a
complete
image and deals with all the memeory management stuff, and another class,
that
is only a tile of the image. Let's call the first MemoryImage, and the
second
ReferenceImage.

My code works well for creating non const and const reference images to a
non
const memory image. Unfortunately, it doesn't work for const memory
images.

The following code illustrates my problem. It's neither stylistically nor
functionally perfect (in fact it's neglecting the identical byte offsets
between consecutive images lines, even though the image widths might be
different.
class MemImage {

public:

// Create image.
MemImage(int width, int height) : m_data(0) {

// check dimensions and allocate aligned memory
}

// Get width of image.
int getWidth() const { return m_width; }

// Get height of image.
int getHeight() const { return m_height; }

// Get pointer to image data.
unsigned char* getData() { return m_data; }

// Get const pointer to image data.
const unsigned char* getData() const { return m_data; }

protected:

unsigned char * m_data;
int m_width;
int m_height;

}; // class MemImage
class RefImage {

public:

RefImage(MemIma ge &image, int x, int y, int width, int height)
: m_data(image.ge tData()+y*image .getWidth()+x), m_width(width),
m_height(height )
{}

// Get pointer to image data.
unsigned char* getData() { return m_data; }

// Get const pointer to image data.
const unsigned char* getData() const { return m_data; }

protected:

unsigned char *m_data;
int m_width;
int m_height;

}; // class RefImage
With the example code above, its possible to do:

void f()
{
MemImage m(400,300);
RefImage r(m, 40, 30, 200, 150);
const RefImge c(m, 40, 30, 200, 150);
}

It's also possible to change image data of m through r.getData()

The code breaks with:
void g()
{
const MemImage m(400,300);
const RefImge c(m, 40, 30, 200, 150);
I can get this to work with an extremly ugly constant cast.

const RefImage c(*(const_cast< MemImage*>( &m )), 40, 30, 200, 150);

I've been trying various things and can't figure it out. It seems that
because RefImage has an
unsigned char* and is trying to initialize it from a const unsigned char*
from unsigned char*.

The only thing I've found so far that works is making m_data public, and
initalizing with

RefImage(const MemImage &image, int x, int y, int width, int height)
: m_data(image.m_ data+y*image.ge tWidth()+x), m_width(width),
m_height(height )
{}

}

Changing the constructor of RefImage to

RefImage(const MemImage &image, int x, int y, int width, int height)
: m_data(image.ge tData()+y*image .getWidth()+x), m_width(width),
m_height(height )
{}

fails due to the initialization of unsigned char m_data with a const
unsigned char.

Changing the member m_data of RefImage to const unsigned char, the
constructor works,
but it's no longer possible to have write access to non const MemImages
via a RefImage.

Can anyone break this vicious circle?

Thanks in advance,

Torsten

Aug 10 '07 #2
Hi!

Torsten Wiebesiek schrieb:
Can anyone break this vicious circle?
No. You have to create a ConstRefImage class. But you can inherit it.

struct ConstRefImage {
ConstRefImage(M emoryImage const& m, ...)
: constImage(m)
{ ... }
MemoryImage const& constImage;
... //some const member functions
};
struct RefImage : ConstRefImage {
RefImage(Memory Image& m, ...)
: ConstRefImage(m ) //cast to const
, image(m)
{ ... }
MemoryImage & image;
... //some non-const member functions
};

void foo(ConstRefIma ge const& image) { ... }
void bar()
{
MemoryImage m(...);
RefImage r(m, ...);
foo(r); //works
};

HTH

Some other advise: you can use std::vector instead of raw memory to
avoid memory management issues. The std::vector is required to allocate
continuous memory. So "&v[0]" will give you the pointer you need.
Getting raw memory management correct is not trivial and should be avoided.

Frank
Aug 10 '07 #3
>Can anyone break this vicious circle?
>
No. You have to create a ConstRefImage class. But you can inherit it.

struct ConstRefImage {
ConstRefImage(M emoryImage const& m, ...)
: constImage(m)
{ ... }
MemoryImage const& constImage;
... //some const member functions
};
struct RefImage : ConstRefImage {
RefImage(Memory Image& m, ...)
: ConstRefImage(m ) //cast to const
, image(m)
{ ... }
MemoryImage & image;
... //some non-const member functions
};

void foo(ConstRefIma ge const& image) { ... }
void bar()
{
MemoryImage m(...);
RefImage r(m, ...);
foo(r); //works
};
Frank, that was a very good hint. Thanks! :-)

I now have the following class hierarchy:

class ConstImage {...};
class Image : public virtual ConstImage {...};

class MemImage : public Image {...};

class ConstRefImage : public virtual ConstImage {...};
class RefImage : public ConstRefImage, public Image {...};

Virtual inheritance, because ConstImage is not abstract but
deals with height and width information. Seems to work.

Some other advise: you can use std::vector instead of raw memory to
avoid memory management issues. The std::vector is required to allocate
continuous memory. So "&v[0]" will give you the pointer you need.
Getting raw memory management correct is not trivial and should be avoided.
I'd love to, but I'm not sure, whether it's really allocating properly
alligned memory. Therefore, I use Intels IPP Memory allocation function:

Ipp<datatype>* ippiMalloc_<mod >(int width, int height, int* pStepBytes);

It's neither very nice nor very convenient, but, well, it's a speed optimized
C library. There are some design drawback, but at least it's very fast.
Regards,

Torsten
Aug 13 '07 #4

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

Similar topics

3
17626
by: Jan13 | last post by:
Hi, I'm new to programming in C++ (using VC6) and ran into the following problem: I want to declare and define a class member variable as 'static const', but something seems to go wrong with the linking.
12
1914
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the board. Since this function does not change anything in the class I declared it as const. To count all pieces of a given color the functions iterates...
26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class Iterator { public : typedef typename Set::value_type T; typedef typename Set::iterator SetIterator; Iterator(Set& container, const SetIterator& it);
3
3586
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a...
4
14424
by: Ant | last post by:
Hi, I'm having trouble trying to define the difference between declaring a field in a class const or static. i've noted that when referencing them from the class that a const is symbolized with a field symbol & a static has a blue box. What is the difference? What is the blue box? Has it got to do with scope or get/setability? Thanks for...
6
2101
by: WLT | last post by:
I'm keeping both a variable, and its default value in a class: class X { public const string DefaultString = "Default Value"; ... public string VarString = DefaultString; } I'm probably not up on subtleties of how 'const' is treated in C#, cause the code compiles fine, but generates a runtime exception.
0
1593
by: Larry Lard | last post by:
This came out of a thread explaining to "BK" about error BC42025 ("Access of shared member through an instance; qualifying expression will not be evaluated"); Frans Clasener then came up with another similar problem, which I believe shows up a bug (well, a problem) in VB2005. As many will know, VB2005 saw the reintroduction of the 'default...
20
4011
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. ...
12
6232
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is: gcc version 4.0.1 (Apple Inc. build 5465). thanks for the help. summary of compile error: --------------------------------------- cpp.C:4:...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7805
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.