Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

c++ - Macros to disallow class copy and assignment. Google -vs- Qt

To disallow copying or assigning a class it's common practice to make the copy constructor and assignment operator private. Both Google and Qt have macros to make this easy and visible. These macros are:

Google:

#define DISALLOW_COPY_AND_ASSIGN(TypeName) 
  TypeName(const TypeName&);   
  void operator=(const TypeName&) 

Qt:

#define Q_DISABLE_COPY(Class) 
  Class(const Class &);      
  Class &operator=(const Class &);

Questions: Why are the signatures of the two assignment operators different? It seems like the Qt version is correct. What is the practical difference between the two?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It doesn't matter. The return type is not part of a function's signature, as it does not participate in overload resolution. So when you attempt to perform an assignment, both declarations will match, regardless of whether you use the return type.

And since the entire point in these macros is that the functions will never get called, it doesn't matter that one returns void.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...