473,386 Members | 2,114 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,386 software developers and data experts.

Can't work with volatile objects?

The canonical way to write a copy-constructor is:

MyClass::MyClass(MyClass const&);

I've been wondering, however, if that should be:

MyClass::MyClass(MyClass const volatile&);
?

Just in case more type specifiers come into the language, we could have a
template such as the following:

template<class Param>
class Strict {
private:
Strict(); /* Can't create an object */

public:

typedef Param const volatile T;
};

, and then have function arguments such as:

MyClass::MyClass(Strict<MyClass>::T &)
{

}

The following code demonstrates how volatile objects aren't catered for:

#include <cstddef>
#include <algorithm>

using std::size_t;
using std::copy;

class MyClass {
private:

typedef int T;

size_t const static len = 10;

T *p;

public:

MyClass() : p(new T[len]) {}

MyClass(MyClass const &from) : p(new T[len])
{
copy(from.p,from.p + len,p);
}

MyClass &operator=(MyClass const &from)
{
copy(from.p,from.p + len,p);
return *this;
}

~MyClass()
{
delete [] p;
}
};

int main()
{
MyClass const volatile obj1;

MyClass obj2(obj1); /* Can't copy */
}
(I just used copy-constructors as an example -- it could be any function:

void Func(MyClass const &);

--

Frederick Gotham
Aug 18 '06 #1
1 1708

Frederick Gotham wrote:
The canonical way to write a copy-constructor is:

MyClass::MyClass(MyClass const&);

I've been wondering, however, if that should be:

MyClass::MyClass(MyClass const volatile&);
No. That would force the implementation to use volatile sematics even
if
the copied object isn't volatile. Forcing const sematics usually has no
performance overhead, so there is little reason to overload the copy
ctor
with a non-const overload. If you need volatile, add it as an overload,
but
keep the efficient non-volatile one.

HTH,
Michiel Salters

Aug 18 '06 #2

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

Similar topics

8
by: David Rasmussen | last post by:
What is the difference in meaning and in consequences between struct S { volatile A* a; }; and struct S
3
by: Mark A. Odell | last post by:
If I have a structure that may point to a volatile "region (e.g. device)" or a context in memory what would be the best way to use the volatile keyword? E.g. a) volatile on struct objects ...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
3
by: nickptar | last post by:
Let's say I have a situation like this: /* begin example.c */ static int* ptr; static void inner_fn(void) { *ptr = 1; }
9
by: d.f.s. | last post by:
In the post below, 'copy constructor?', the answers refer to an object declared as const volatile. Now I'm confused. Are those terms not mutually exclusive? const='Hey compiler! This is not...
17
by: Grizlyk | last post by:
Hello. What can be optimised in C++ code and how i can garantee stable behaviour below 1. Are expression "auto volatile" can deny removing as "unused temporary" like this: auto volatile...
4
by: Shraddha | last post by:
What does volatile keyword actually used for?
3
by: Rakesh Kumar | last post by:
Hi - I am actually trying to get my feet in multi-threaded C++ programming. While I am aware that the C++ standard does not talk about threads (at least, for now - in C++03) - my question is more...
8
by: Markus | last post by:
Hello everyone. Recently I stumbled upon an interesting problem related to thread-parallel programming in C (and similarily C++). As an example assume a simple "buffer" array of size 8, e.g....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.