本文介绍了Const成员初始化列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如果我犯了愚蠢的错误,请从我更复杂的案例中创建这个简单的 示例,请不要太苛刻。 /> 假设我有这样的课程: 班级预订 { const字符串标题; const字符串作者; const int pages; const double price; 静态地图< string,BOOK> MyBooks; BOOK(const string& title_,const string& author_, const int pages_,const double price_); BOOK(const string& title_); const BOOK& findByTitle(const string& title); } 我在地图上填写由4参数构造函数创建的书籍。我使用 findByTitle在地图中查找书籍。我的问题是使用 构造函数来获取标题。关于定义这个 的唯一方法似乎是: BOOK :: BOOK(const string& title_) : title(title_), 作者(findByTitle(title _)。作者) 页面(findByTitle(title _)。pages) price(findByTitle (标题_)。价格) {} 问题是findByTitle被调用3次。这不是非常有效的b $ b。如果我的成员是非const我可以做 BOOK :: BOOK(const string& title_) :title(title_) { BOOK book = findByTitle(title_); author = book.author; pages = book.pages; price = book.price; } 但是因为我的书没有突然改变他们的习惯/> 作者,我认为那些成员*应该是const。任何优雅的 解决方案? 谢谢, WolframHi,please don''t be too harsh if I made stupid errors creating this simpleexample from my more complex case.Suppose I have a class like this:class BOOK{const string title;const string author;const int pages;const double price;static map<string, BOOK> MyBooks;BOOK(const string& title_, const string& author_,const int pages_, const double price_);BOOK(const string& title_);const BOOK& findByTitle(const string& title);}I fill the map with books created by the 4-argument constructor. I usefindByTitle to look up books in the map. My problem is with theconstructor that takes a title. About the only way to define thisseems to be:BOOK::BOOK(const string& title_): title(title_),author(findByTitle(title_).author)pages(findByTitle(title_).pages)price(findByTitle(title_).price){}The problem is that findByTitle is called 3 times. That''s not veryefficient. If my members were non-const I could doBOOK::BOOK(const string& title_): title(title_){BOOK book = findByTitle(title_);author = book.author;pages = book.pages;price = book.price;}But since my books don''t have a habit of suddenly changing theirauthor, I think those members *should* be const. Any elegantsolutions?Thanks,Wolfram推荐答案 您必须回答这个问题:为什么我需要从单个构建另一个 对象''title''当我已经有一个坐在 地图< string,BOOK>?只要你能回答这个问题,我们就能够给你一个优雅的解决方案。交易? VictorYou have to answer the question: why do I need to construct anotherobject from a single ''title'' when I already have one sitting in themap<string, BOOK>? As soon as you can answer that, we''ll be able togive you an elegant solution. Deal?Victor 假设班级用户想说: (假设书已经在地图中,否则会出错。 BOOK stroustrup(The C ++ Programming Language); cout<< Stroustrup有 << stroustrup.pages<< "页" <<结束; 或我有 LIBRARY :: addBook(BOOK book); myLibrary.addBook(The C ++ Programming Language); 我已经发现在这里我需要一个BOOK :: BOOK(const char * title_)(含 char *而不是string)构造函数。我认为原因是否则 我有一个带有两个用户定义转换的隐式转换链......? WolframSuppose the user of the class wants to say:(assuming the book is already in the map, error otherwise)BOOK stroustrup("The C++ Programming Language");cout << "Stroustrup has " << stroustrup.pages << " pages" << endl;or I haveLIBRARY::addBook(BOOK book);myLibrary.addBook("The C++ Programming Language");I already found that here I need a BOOK::BOOK(const char* title_) (withchar* instead of string) constructor. I think the reason is that otherwiseI have an implicit conversion chain with two user-defined conversions...?Wolfram 假设班级用户想说:(假设这本书已经在地图上,否则错误) BOOK stroustrup(The C ++ Programming Language); cout<< Stroustrup有 << stroustrup.pages<< "页" << endl; 或者我有 LIBRARY :: addBook(BOOK book); myLibrary.addBook(The C ++ Programming Language) ; 我已经发现在这里我需要一个BOOK :: BOOK(const char * title_)(用 char *而不是string)构造函数。我认为原因是否则我有一个带有两个用户定义转换的隐式转换链......? Wolfram Suppose the user of the class wants to say: (assuming the book is already in the map, error otherwise) BOOK stroustrup("The C++ Programming Language"); cout << "Stroustrup has " << stroustrup.pages << " pages" << endl; or I have LIBRARY::addBook(BOOK book); myLibrary.addBook("The C++ Programming Language"); I already found that here I need a BOOK::BOOK(const char* title_) (with char* instead of string) constructor. I think the reason is that otherwise I have an implicit conversion chain with two user-defined conversions...? Wolfram 在我看来,addBook(至少在第二种情况下)应该为给定标题的书执行 查找,如果找不到则会出错。然后 它可以根据找到的实例创建一个新实例(或复制 - 创建它,或者使用对现有实例的引用)。我不确定你想要什么?在第一种情况下发生,因为你还没有提供没有 参数的构造函数,而你是实际上增加了一个未知的预订到图书馆。 (另外,地图不应该是你的图书馆班级的成员,而不是图书类本身的成员吗?这看起来很奇怪有一本书里面有一本书的书,不是吗?不是吗?) -HowardIt seems to me that addBook (in the second case at least) should do thelookup for the book given the title, and error out if it is not found. Thenit can create a new instance (or copy-create it, or use a reference to thethe existing one) based on the one it finds. I''m not sure what you want tohappen in the first case, since you haven''t provided a constructor with noparameters, and you''re in effect adding an "unknown" book to the library.(Also, shouldn''t the map be a member of your library class, not a member ofthe book class itself? It seems strange to have a book that has acollection of books in it, doesn''t it?)-Howard 这篇关于Const成员初始化列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 08:14