473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

type casting of a ptr to a c struct

I have a c struct from old code that cannot be modified and I am trying to
write a wrapper C++ class around it.
This class is often passed as a pointer to some c functions of a library and I
wanted to keep my new class transparent and compatible with those functions.
That is, when using the code, I should be able to do either:
oldVector xyz;
function( &xyz );

myVector xyz;
function( &xyz );

My original implementation solved the issue using inheritance. That is, in
pseudo code:

// old code
typedef struct {float x, y, z;} oldVector;

/// my class
class myVector : public oldVector
{
/// new methods here
};

This worked fine for my needs, but I wanted to change myVector class to be a
template which would likely prevent the inheritance.

I know the alignment in the compilers I will be should result in an identical
memory layout.
I was wondering if it was possible to use type casting to work around it.

Basically, I was thinking something along the lines of:

template <class T> class Vec3
{
public:
T x, y, z;
// other methods....
};

class myVector : public Vec3< float >
{
operator oldVector();

// but for a pointer.... would this be valid????
operator *oldVector();
};
Would this be valid, or terrible ideas so far and something else is likely
better?
Jul 19 '05 #1
3 5802
"John Carson" <do***********@datafast.net.au> wrote in message
news:3e******@usenet.per.paradox.net.au

#include <iostream>


An anomaly resulting from some late editing. It should be

#include <cstdio>

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 19 '05 #2
polymorphic_cast<> of the boost library might help better.

Jul 19 '05 #3
Cgacc20 wrote in news:20***************************@mb-m26.aol.com:
I have a c struct from old code that cannot be modified and I am
trying to write a wrapper C++ class around it.
This class is often passed as a pointer to some c functions of a
library and I wanted to keep my new class transparent and compatible
with those functions. That is, when using the code, I should be able
to do either:
oldVector xyz;
function( &xyz );

myVector xyz;
function( &xyz );

My original implementation solved the issue using inheritance. That
is, in pseudo code:

// old code
typedef struct {float x, y, z;} oldVector;

/// my class
class myVector : public oldVector
{
/// new methods here
};

This worked fine for my needs, but I wanted to change myVector class
to be a template which would likely prevent the inheritance.

I know the alignment in the compilers I will be should result in an
identical memory layout.
I was wondering if it was possible to use type casting to work around
it.

This is possible, but lets try somthing different first.
Basically, I was thinking something along the lines of:

template <class T> class Vec3
{
public:
T x, y, z;
// other methods....
};

class myVector : public Vec3< float >
{
operator oldVector();

// but for a pointer.... would this be valid????
operator *oldVector();
};


I'm guessing from what you say that you want to eventually make
myVector a class template as well, If so it would be usefull to
know what you intend to paramitise it on? I've guessed Flaot
bellow.

Anyway:

#include <iostream>
#include <ostream>
#include <vector>

#include <cstddef> /* for std::size_t */
/* Your old code:
*/
struct oldVector { float x, y, z; };

/* new template:
*/
template <typename Float>
class vec3
{
public:
// new stuff here

public: // private: /* maybe? */
Float x, y, z;
};

/* specialization for backward compatibility:
*/
template <>
class vec3< float >: public oldVector
{
public:
// all your new stuff again

/* uncomment _make_too_big_ to see check the compile time assert works:
*/
//char _make_too_big_;
};
template <std::size_t szA, std::size_t szB>
struct ASSERT_EQUAL_SIZE_helper;

template <std::size_t szT>
struct ASSERT_EQUAL_SIZE_helper< szT, szT>
{
static void is_equal_size() {}
};
template <typename A, typename B>
struct ASSERT_EQUAL_SIZE:
ASSERT_EQUAL_SIZE_helper< sizeof(A), sizeof(B) >
{
};
template <typename Float>
class myVector : public vec3< Float >
{
public:

myVector(Float xx, Float yy, Float zz)
{
x = xx;
y = yy;
z = zz;
}

~myVector(); // for assert
};

template <typename Float>
inline myVector< Float >::~myVector()
{
/* do nothing dtor (no assert needed) */
}

template <>
myVector< float >::~myVector()
{
/* check the size of *this is the same as oldVector
*/
ASSERT_EQUAL_SIZE< myVector< float >, oldVector >
::
is_equal_size()
;
}
void test(oldVector *xyz, std::size_t n)
{
for (std::size_t i = 0; i < n; ++i)
{
std::cout
<< xyz[i].x
<< ","
<< xyz[i].y
<< ","
<< xyz[i].z
<< std::endl
;
}
}

int main()
{
typedef float float_t;
std::vector< myVector< float_t > > a;
myVector< float_t> v(1, 2, 3);

a.push_back(v);
a.push_back(v);
a.push_back(v);

test(&a[0], a.size());
}
The obove compiled on g++ (gcc 3.2) and MSVC 7.1.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
2
by: Dave | last post by:
Hello all, I am creating a linked list implementation which will be used in a number of contexts. As a result, I am defining its value node as type (void *). I hope to pass something in to its...
3
by: Greg Roberts | last post by:
Hi I am writing a program using the SortedList class . Please contain laughter if i am doing something stupid., I am using a struct as the value part to store a range of information against a...
6
by: crook | last post by:
I have code below and it works properly but when I'm compiling it with "--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids casting nonscalar to the same type". How can I change this...
5
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran...
25
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
2
by: pallav | last post by:
I'm using an old sparse matrix C library in my C++ code and I'd like to know how to downcast a boost::shared_ptr to char *. The sparse matrix data structure is like this: struct...
4
by: Schkhan | last post by:
Hi, I am sturggling with a peace of code mainly its about casting a pointer to one type of structure to a pointer to another type of structure. the confusion begins with the following...
5
by: Chad | last post by:
Keith made the following comment " You can convert an int*, or an int**, or an int***, or ... to void* without loss of information -- except that you lose the type information, which you have to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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...
0
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,...

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.